magazine.js 11.1 KB
/*
 * @fileOverview magazine页的JS
 * @author fei.hong@yoho.cn
 * @date 2014-05-30
 */

    var $ = require("jquery");
    var dot = require("dot");
    var json = require("json");
    var share = require("yoho-share");
    var swiper = require('yoho.swiper');

    require('../plugin/lazyloadImage');
    require('../plugin/imgSlider');
    require('jquery.cycle');
    require('../plugin/imgZoom');

    
    /**
     * 初始化
     */
    exports.init = function()
    {
        var shareUrl = $(".share").attr("data-link");
        share.init({
            shareUrl:shareUrl,
            img:$(".mags-slide li").eq(0).find("img").attr("src"),
            sinaText:'随时引领潮流生活,我正在看《YOHO!潮流志》'+$(".date-price").find("span").text()+'期点杂志,每月7日,17日,27日出刊哦~',
            fbTextFunction:function(){
                return {
                    des:'随时引领潮流生活,我正在看《YOHO!潮流志》'+$(".date-price").find("span").text()+'期点杂志,每月7日,17日,27日出刊哦~',
                    name:'随时引领潮流生活,我正在看《YOHO!潮流志》'+$(".date-price").find("span").text()+'期点杂志,每月7日,17日,27日出刊哦~',
                    caption:'随时引领潮流生活,我正在看《YOHO!潮流志》'+$(".date-price").find("span").text()+'期点杂志,每月7日,17日,27日出刊哦~',
                    url:location.href
                }
            },
            tweetText:'随时引领潮流生活,我正在看《YOHO!潮流志》'+$(".date-price").find("span").text()+'期点杂志,每月7日,17日,27日出刊哦~',
            qqText:'随时引领潮流生活,我正在看《YOHO!潮流志》'+$(".date-price").find("span").text()+'期点杂志,每月7日,17日,27日出刊哦~'
        });
        //显示控制箭头
        (function(){
            var timer;
            $(".column-slide").on("mouseenter",function(){
                $(".column-slide").find(".slide-ctrl").hide();
                if($(this).find("li").size()<=4) return;
                clearTimeout(timer);
                $(this).find(".slide-ctrl").show();
            }).on("mouseleave",function(event){
                var $that = $(this);
                timer=setTimeout(function(){
                    $that.find(".slide-ctrl").hide();   
                },1000)
            });

            $(".column-slide .slide-ctrl").on("mouseenter",function(){
                clearTimeout(timer);
                $(this).show();
            }).on("mouseleave",function(){
                timer=setTimeout(function(){
                    $(this).hide();  
                },100)
            });
            
        })();    
        $("img.lazy").lazyload({ 
            effect : "fadeIn"
        });

        var magazineSlide = new swiper('.mags-slide .swiper-container',{
            pagination: '.slide-ctrl-tabs',
            wrapperClass:'slide-piclist',
            slideClass:'magazine-swiper',
            paginationClickable: true,
            autoplay:5000,
            loop:true,
            slidesPerView: 'auto',
            slideElement: 'li'
        })
        /*var magazineSlide=new swiper('.magazine-list .swiper-container',{
            //slidesPerView: 'auto',
            //slideElement: 'li'
            simulateTouch:false,
            wrapperClass:'slide-piclist',
            slideClass:'magazine',
            paginationClickable: true,
            slidesPerView: 'auto',
            slideElement: 'li',
            onSlideChangeEnd: fallSwiperItems_buy
        });

        function fallSwiperItems_buy(){
            var currentSlide=arguments[0];
            _fallSwiperItems(currentSlide,"buy");
       }

       function fallSwiperItems_rule(){
            var currentSlide=arguments[0];
            _fallSwiperItems(currentSlide,"rule");
       }
       var currentPageNumber=1;
       function _fallSwiperItems(currentSlide,type){
            var url="";
            if(type=="buy") {
                url="/channel/magazine/page?type=1&p=";
            } else if( type=="zine") {
                url="/channel/magazine/page?type=2&p=";
            } else {
                url="/channel/magazine/rules?p=";
            }
            
            var totalLength=$(currentSlide.container).find(".swiper-slide").length;
            var currentIndex=currentSlide.activeIndex;
            if(totalLength-currentIndex<=6) {
                $.ajax({
                    url:YohoConfig.mainUrl+url+(currentPageNumber++),
                    cache:false,
                    success:function(data){
                        for(var i=0;i<data.data.length;i++) {
                            var msg=data.data;
                            var newAppend=currentSlide.createSlide('<li class="'+_this.options.itemClass+' swiper-slide"><a href="javascript:;"><img src="" data-slide="'+msg[i].pics+'" data-original="'+msg[i].cover+'"/><span>'+msg[i].title+'</span></a></li>');
                            newAppend.append();
                        }
                        var oLi=$(currentSlide.container).find(".swiper-slide");
                        var LastSlide = currentSlide.getLastSlide();
                        currentSlide.reInit();

                    }
                });
            }
       }*/

        /*$(".magazine-list").find(".slide-ctrl").find('.slide-ctrl-prev').on('click', function(e){
            e.preventDefault();
            magazineSlide.swipePrev();
        });
        $(".magazine-list").find(".slide-ctrl").find('.slide-ctrl-next').on('click', function(e){
            e.preventDefault()
            magazineSlide.swipeNext();
            $(".magazine").find("img").lazyload();
        });*/
        /*$(".mags-slide .slide-piclist").cycle({
            fx: 'scrollHorz',
            speed: 300,
            timeout: 5000,
            pager: '.mags-slide .slide-ctrl-tabs'
        });*/

        $.ajax({
            type:'GET',
            url:'/channel/magazine/ezine',
            success:function(response){
                var data = response.data;
                var len = data.length;
                var item ='';
                var ulNum = Math.ceil(len/16);
                var ulItem ='';
                var ulAnimateIndex = 0;
                
                $(".mags-column .slide-box").html('<div class="slide-piclist-wrap clearfix" style="position:relative;top:0;left:0;"></div>');
                
                //添加4个ul
                for(var r=0;r<ulNum;r++){
                    ulItem += '<ul class="slide-piclist clearfix"></ul>';
                }
                $(".slide-piclist-wrap").html(ulItem);

                var animateWidth = $(".mags-column").find(".slide-piclist").outerWidth();
                var slideWrapWidth =  animateWidth * ulNum;
                $(".slide-piclist-wrap").css({"width":slideWrapWidth});
    
                //分别插入数据到4个ul
                for(var i=0;i<ulNum;i++){
                    var itemIndexStart = i*16;
                    var itemIndexEnd = i*16+16;
                    item = '';
                    if(itemIndexEnd>len){
                        itemIndexEnd=len;
                    }
                    for(var j = itemIndexStart;j<itemIndexEnd;j++){
                        item += '<li data-type='+data[j].magazineType+'><a href="javascript:;" title="'+data[j].releaseDate+'"><div class="img-wrap"><img src="'+data[j].cover+'" alt="" /></div><span>'+data[j].releaseDate+'</span></a></li>';
                    }
                    
                    $(".slide-piclist-wrap").find(".slide-piclist").eq(i).html(item);
                }

                //滚动
                $(".mags-column").find(".slide-ctrl-prev").hide();
                $(".mags-column").on("click.next",".slide-ctrl-next",function(){
                    if(ulAnimateIndex>=ulNum-1) return;
                    if($(".slide-piclist-wrap").is(":animated")) return;
                    if(ulAnimateIndex==ulNum-2){
                        $(this).hide();
                    }
                    $(this).siblings(".slide-ctrl-prev").show();
                    ulAnimateIndex++;
                    $(".slide-piclist-wrap").stop().animate({"left":-animateWidth*ulAnimateIndex},500);
                });

                $(".mags-column").on("click.prev",".slide-ctrl-prev",function(){
                    if(ulAnimateIndex<=0) return;
                    if($(".slide-piclist-wrap").is(":animated")) return;
                    if(ulAnimateIndex==1){
                        $(this).hide();
                    }
                    $(this).siblings(".slide-ctrl-next").show();
                    ulAnimateIndex--;
                    $(".slide-piclist-wrap").stop().animate({"left":-animateWidth*ulAnimateIndex},500);
                });

                /*for(var i=0;i<len;i++){
                    item += '<li data-type='+data[i].magazineType+'><a href="javascript:;" title="'+data[i].releaseDate+'"><div class="img-wrap"><img src="'+data[i].cover+'" alt="" /></div><span>'+data[i].releaseDate+'</span></a></li>';
                }
                $(".mags-column .slide-piclist").html(item);
                $(".column-slide").imgSlider();     
                */

                $(".mags-column .slide-piclist").find("li").each(function(){
                    if($(this).data("type") ==2){
                        $('<em class="border"></em><i class="special-icon"></i>').appendTo($(this).find(".img-wrap"));
                    };
                });
            }
        })

        $(".column-slide.magazine-list").imgSlider({
            itemClass:'magazine'
        });
        $(".column-slide.rule").imgSlider();
        //ipad横竖屏切换
        $(window).resize(function(){
            clearTimeout(timer);
            var timer = setTimeout(function(){
                $(".column-slide.rule").imgSlider('resize');
                $(".column-slide.magazine-list").imgSlider('resize');
            },0)
            
        });
        $('.column-slide.rule').ImgZoom({
            imgSrcAttr:'data-imgZoom'
        });

        $(".slide-piclist").on("click",".magazine",function(){
            if(!$(this).find("img").attr("data-slide")) return;
            if($(this).find("ul").size()>=1) return;
            var data = decodeURIComponent($(this).find("img").attr("data-slide"));
            var arr =eval("("+data+")");
            var len  = arr.length;
            var $picList = $('<ul style="display:none;" class="magazine-content"></ul').appendTo($(this));
            var item =''

            for(var i=0;i<len;i++){
                item += '<li><img src="" data-imgzoom="'+arr[i]+'" alt="" /></li>';

            }
            $(item).appendTo($picList);
            $(this).ImgZoom({
                isContainFather: true
            });
        });

        $(".column-slide.rule").on("mouseenter","li",function(){
            var gifSrc = $(this).find("img").attr("data-imgzoom");
            $(this).find("img").attr("src",gifSrc);
        }).on("mouseleave","li",function(){
            var staticSrc = $(this).find("img").attr("data-original");
            $(this).find("img").attr("src",staticSrc);
        })
    }