home.js 5.19 KB
/**
 *  首页
 * @author: liangzhifeng<zhifeng.liang@yoho.cn>
 * @date: 2015/10/12
 */
var $ = require('yoho.zepto'),
    Swiper = require('yoho.iswiper'),
    lazyLoad = require('yoho.zeptolazyload'),
    bannerSwiper,
    recommendSwiper,
    trendTopicSwiper,
    goodsSwiper;

var requestFrame,
    thisFunc,
    start = 0,
    i,
    swiperClass,
    supportCss3,
    $logotrans = $('.home-header .logo'),
    isen = true;

require('./maybe-like');

lazyLoad($('img.lazy'));

//$('img:in-viewport').trigger('appear');

//点击首页汉堡menu图标,滑出侧栏导航
$('.nav-btn').on('click', function (event) {
    if (!$(this).hasClass('menu-open')) {
        $('.mobile-wrap').addClass('menu-open');
        $('.overlay').addClass('show');
        $('.side-nav').addClass('on');

        //设置boy高宽,页面不能上下滑动
        $('body').css({
            height: $(window).height(),
            width: '100%',
            overflow: 'hidden'
        });
    }
    event.stopPropagation();
});

//点击页面主体,收起侧栏导航及二级导航
$('.mobile-wrap').on('click', function () {
    if ($(this).hasClass('menu-open')) {
        $('.mobile-wrap').removeClass('menu-open');
        $('.overlay').removeClass('show');
        $('.sub-nav').removeClass('show');
        $('.side-nav').removeClass('on');
        $('body').css({
            height: 'auto',
            overflow: 'auto'
        });
    }
});

//点击一级导航,弹出二级导航
$('.side-nav').on('click', 'li', function () {
    if ($(this).find('.sub-nav').size() > 0) {
        $('.sub-nav').removeClass('show');
        $(this).find('.sub-nav').addClass('show');
    }
});

//返回一级导航,收起二级导航
$('.sub-nav').each(function () {
    $(this).find('li').eq(0).on('click', function (e) {
        $('.sub-nav').removeClass('show');
        e.stopPropagation();
    });
});

//二级导航样式控制
$('.sub-nav').on('mouseenter', 'li', function () {
    if ($(this).index() !== 0) {
        $(this).addClass('current').siblings().removeClass('current');
    }
});

//头部banner轮播
if ($('.banner-swiper').find('li').size() > 1) {
    bannerSwiper = new Swiper('.banner-swiper', {
        loop: true,
        autoplay: 3000,
        autoplayDisableOnInteraction: false,
        paginationClickable: true,
        slideElement: 'li',
        pagination: '.banner-top .pagination-inner'
    });
}

//热门品牌滑动
// hotBrandsSwiper = new Swiper('.brands-swiper', {
//     grabCursor: true,
//     slidesPerView: 'auto',
//     wrapperClass: 'brands-list',
//     slideElement: 'li'
// });

//推荐搭配滑动
recommendSwiper = new Swiper('.recommend-swiper', {
    grabCursor: true,
    slidesPerView: 'auto',
    wrapperClass: 'recommend-list',
    slideElement: 'li'
});

//潮品话题轮播
if ($('.trend-topic-swiper').find('li').size() > 1) {
    trendTopicSwiper = new Swiper('.trend-topic-swiper', {
        loop: true,
        autoplay: 3000,
        autoplayDisableOnInteraction: false,
        paginationClickable: true,
        slideElement: 'li',
        pagination: '.trend-topic-content .pagination-inner'
    });
}

//潮流上装/经典裤装等轮播
$('.category-swiper').each(function (i, index) {
    swiperClass = 'category-swiper' + i;
    $(this).addClass(swiperClass);
    if ($('.' + swiperClass).find('.swiper-slide').size() > 1) {
        goodsSwiper = new Swiper('.' + swiperClass, {
            loop: true,
            autoplay: 3000,
            autoplayDisableOnInteraction: false,
            paginationClickable: true,
            slideElement: 'li',
            pagination: '.' + swiperClass + ' .pagination-inner'
        });
    }
});


//回到顶部
// $('.back-to-top').bind('touchstart', function (e) {
//     e.preventDefault();
//     $(window).scrollTop(0);
// });

//关闭头部下载浮层
$('.header-download').on('click', '.close-btn', function () {
    $(this).parent().remove();
});


//logo动画
requestFrame = (function () {
    var tempFunc = null,
        prefixList = ['webkit', 'moz', 'ms'];

    for (i = 0; i < prefixList.length; i++) {
        thisFunc = prefixList[i] + 'RequestAnimationFrame';
        if (window[thisFunc]) {
            supportCss3 = true;
            tempFunc = thisFunc;
        }
    }

    if (supportCss3) {
        return function (callback) {
            window[tempFunc](callback);
        };
    }
    return function (callback) {
        window.setTimeout(callback, 67);
    };
})();

function tsAnimate() {
    start = start + 10;
    $logotrans.css({
        transform: 'rotateX(' + start + 'deg)',
        '-webkit-transform': 'rotateX(' + start + 'deg)',
        '-moz-transform': 'rotateX(' + start + 'deg)'
    });
    if (start / 90 % 2 === 1) {
        if (isen) {
            $logotrans.addClass('animate');
            isen = false;
        } else {
            $logotrans.removeClass('animate');
            isen = true;
        }
    }
    if (start / 90 % 2 === 0 && start % 360 !== 0) {
        window.setTimeout(tsAnimate, 3000);
    } else {
        if (start % 360 === 0) {
            window.setTimeout(tsAnimate, 3 * 60 * 1000);
        } else {
            requestFrame(function () {
                tsAnimate();
            });
        }
    }
}

tsAnimate();