mobile.js 8.96 KB
/**
 * @h5页面js
 * @author:liuyue
 * @date:2015-05-20
 **/

define('mobile', function(require, exports) {
    var $ = require('jquery'),
        swipe = require("plugins/swipe"),
        share = require("plugins/share"),
        common = require("common"),
        swiper = require("plugins/idangerous.swiper"),
        mlellipsis = require("plugins/mlellipsis");
    require("plugins/imgZoom");
    require("plugins/lazyload");
    exports.common = function() {
        var downloadH,
            scrollTop;
        if ($('.header-download').size() > 0) {
            downloadH = $('.header-download').height();
            //顶部app下载位置
          //  console.log(!$('.search-wrap input').hasClass('show'));

            $(window).on('scroll', function() {
                scrollTop = $(window).scrollTop();
                if (!$('.search-wrap input').hasClass('show')) {
                    if (scrollTop >= downloadH) {
                        $('.header-download').addClass('fix');
                        $('.header-download-wrap').addClass('show');
                    } else {
                        $('.header-download').removeClass('fix');
                        $('.header-download-wrap').removeClass('show');
                    }
                }
            });
        }
    };

    exports.index = function() {
        //大banner滑动
        swipe.init({
            slideBox: '.slide-box',
            pagination: '.dib a',
            activeClass: 'on',
            auto: 3000,
            continuous: true
        });
        if ($('.slide-navigator .dib').find('a').size() <= 1) {
            $('.slide-navigator').hide();
        }
        //频道点击效果
        $('.channel-list').on('tap', 'li', function() {
            $(this).addClass('current').siblings().removeClass('current');
        });
    };
    exports.activity = function() {
        $('.content-tab').on('click', 'li', function() {
            var nowIndex = $(this).index();
            $(this).addClass('current').siblings().removeClass('current');
            $('.content-main').hide().eq(nowIndex).show();
        });

    };
    exports.brand = function() {
        var bannerSwiper;
        var getBrand = function() {
            var brand = $.trim($("input[type='text']").val());
            var template = '<ul class="brand-list cooperation-list clearfix">{li}</ul>';
            var noSearchTemplate = '<div class="no-search" style="display:block;"><p>未搜索到“{brand}”的相关品牌</p><a href="javascript:;" class="all_brand"><查看全部品牌</a></div>';
            var html = '';
            $.ajax({
                type: "post",
                url: "/brand/getbrand",
                data: {
                    brand: brand
                },
                dataType: 'json',
                success: function(data) {
                    var brands = data.data;
                    if (data.code == 200 && brands != "") {
                        $.each(brands, function(k, v) {
                            html += '<li><a href="/brand/detail/id/' + v.id + '" title="' + v.name + '"><div class="img-box">' +
                                '<img src="' + common.getImages(v.logo, '0145x0097', 'blogimg', 'primary') + '" alt="" alt="' + v.name + '"></div>' +
                                '<p>' + v.name + '</p></a></li>';
                        });
                        $(".brand-content").html(template.replace('{li}', html));
                    } else {
                        $(".brand-content").html(noSearchTemplate.replace('{brand}', brand));
                    }
                }
            });
        };

        //大banner滑动
        if ($('.slide-box').find('.box').size() > 1) {
            bannerSwiper = new swiper('.slide-box', {
                wrapperClass: 'slide-wrap',
                slideClass: 'box',
                loop: true,
                pagination: '.slide-navigator .dib',
                paginationElement: 'a'
            });
        }
        //banner只有一张图时不显示小圆点
        if ($('.slide-navigator .dib').find('a').size() <= 1) {
            $('.slide-navigator').hide();
        }

        //搜索框获得焦点
        $('.search-wrap input').on('focus', function() {
            var offsetTop = $('.search-wrap').offset().top;
            $('.overlay').addClass('show');
            $(this).removeClass('show').addClass('show');

            $('body,html').animate({
                scrollTop: offsetTop
            });
        });
        $('.search-wrap').find('input').on('keyup', function(event) {
            var keycode = event.which;
            if (keycode == 13) {
                $('.search-wrap input').blur();
            }
        });
        $('.search-btn').on('tap', function() {
            $('.search-wrap input').focus().addClass('hasVal');
        });
        //获取所有品牌
        $(".all_brand").live("tap", function() {
            $("input[type='text']").val("");
            getBrand();
        });
        $('.search-wrap input').on('blur', function() {
            var txtInput = $(this);
            $('.overlay').removeClass('show');
            if (txtInput.val() === '') {
                txtInput.removeClass('hasVal');
            } else {
                txtInput.addClass('hasVal');
            };
            getBrand();
        });
        getBrand();
    };

    exports.scene = function() {
        $('.scene-tab').on('tap', 'li', function() {
            var nowIndex = $(this).index();
            if ($(this).hasClass('disable')) return;
            $(this).addClass('current').siblings().removeClass('current');
            $('.main-layout').removeClass('current').eq(nowIndex).addClass('current');
        });
        $('.scene-content').find('.layout-item').each(function() {
            $(this).imgZoom();
        });
        //获取手机屏幕高宽
        var width = $(window).width();
        //
        $("img.lazy").each(function(){
        	$(this).attr("data-original", $(this).attr("data-original")+"?imageView/2/w/"+width);
        });
        
        $("img.lazy").lazyload();
    };

    exports.detail = function() {
        var shareUrl = $(".share").attr("data-link"),
            shareCover = $(".share").attr("cover-url"),
            detailTitle = $('.detail-title').find('.title'),
            detailSubtitle = $('.detail-title').find('.subtitle');
        mlellipsis.init();

        //大banner滑动
        swipe.init({
            slideBox: '.slide-box',
            pagination: '.dib a',
            activeClass: 'on',
            auto: 3000,
            continuous: true
        });

        if ($('.slide-navigator .dib').find('a').size() <= 1) {
            $('.slide-navigator').hide();
        }
        //截字
        $('.related-post').find('.content').each(function() {
            $(this)[0].mlellipsis(2);
        });
        //分享
        share.init({
            shareUrl: shareUrl,
            img: shareCover,
            sinaText: '【YOHOOD 2015】' + detailTitle.text() + '\r\n' + detailSubtitle.text() + '@YOHO潮流志',
            fbTextFunction: function() {
                return {
                    des: '【YOHOOD 2015】' + detailTitle.text() + '\r\n' + detailSubtitle.text(),
                    name: '【YOHOOD 2015】' + detailTitle.text() + '\r\n' + detailSubtitle.text(),
                    caption: '【YOHOOD 2015】' + detailTitle.text() + '\r\n' + detailSubtitle.text(),
                    url: location.href
                }
            },
            tweetText: '【YOHOOD 2015】' + detailTitle.text() + '\r\n' + detailSubtitle.text(),
            qqText: '【YOHOOD 2015】' + detailTitle.text() + ' ' + detailSubtitle.text() + '#YOHOOD 2015'
        });
    };

    exports.show = function() {
        $(".layout-item").bind("click", function(event) {
            if ($(this).attr("isopen") == 1) {
                $(this).find(".image-box .pic-tip").each(function() {
                    $(this).css({
                        display: "none"
                    });
                });
                $(this).attr("isopen", 0);
            } else {
                var imageWidth = $(window).width(),
                    imageHeight = 320;
                $(this).find(".image-box .pic-tip").each(function() {
                    var style_left = parseInt($(this).attr("x")) / 10000 * imageHeight;
                    var style_top = parseInt($(this).attr("y")) / 10000 * imageWidth;
                    $(this).css({
                        top: style_top,
                        left: style_left,
                        display: "block"
                    });

                });
                $(this).attr("isopen", 1);
            }
        });
    };

    exports.news = function() {
        $('select').on('change', function() {
            /* var nowIndex = $('option').not(function() {
                 return !this.selected
             }).index();
             $('.news-main').removeClass('current').eq(nowIndex).addClass('current');*/
            window.location.href = $(this).val();
        })
    };
});