home.page.js 10.2 KB
/**
 *  首页
 * @author: liangzhifeng<zhifeng.liang@yoho.cn>
 * @date: 2015/10/12
 */
var $ = require('yoho-jquery'),
    Swiper = require('yoho-swiper'),
    lazyLoad = require('yoho-jquery-lazyload'),
    fastclick = require('yoho-fastclick'),
    noticeScroll = require('../plugin/notice-scroll'),
    tip = require('../plugin/tip');

var $mobileWrap = $('.mobile-wrap'),
    $overlay = $('.overlay'),
    $sideNav = $('.side-nav'),
    $subNav = $('.sub-nav'),
    $logotrans = $('.home-header .logo'),
    start = 0,
    swiperClass,
    isen = true;

var C_ID;

require('../common');
fastclick.attach(document.body);

lazyLoad($('img.lazy'));
noticeScroll('.notice', $('.notice').data('time') * 1000);

$('.nav-btn').on('touchstart', function() {
    $sideNav.css('pointer-events', 'none');
    $mobileWrap.addClass('menu-open');
    $overlay.show().css('opacity', 0.3);
    $sideNav.addClass('on');

    setTimeout(function() {
        $sideNav.css('pointer-events', 'auto');
    }, 400);

    return false;
});

function hideSideBar() {
    if ($mobileWrap.hasClass('menu-open')) {
        $mobileWrap.removeClass('menu-open');
        $overlay.hide();
        $('.sub-nav').removeClass('show');
        $sideNav.removeClass('on');
    }
}

$overlay.on('touchstart', function() {
    hideSideBar();
    return false;
});

$sideNav.on('touchmove scroll', function() {
    return false;
});

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

    if (e.target.pathname === location.pathname) {
        hideSideBar();
        return false;
    }
});

// 返回一级导航,收起二级导航
$subNav.each(function() {
    $(this).find('li').eq(0).on('click', function() {
        $('.sub-nav').removeClass('show');
        return false;
    });
}).on('touchstart', function(e) {
    if (e.currentTarget !== e.target) {
        $subNav.find('li').removeClass('current');
    }
});

// 侧边栏点击背景色变化
function highlight($elem) {
    $elem.find('li').on('touchstart', function() {
        $elem.find('.highlight').removeClass('highlight');
        $(this).addClass('highlight');
    }).on('touchend touchcancel', function() {
        $(this).removeClass('highlight');
    });
}
highlight($sideNav);
highlight($subNav);


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

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

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

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

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

// 新人专享轮播
if ($('.fresh-list-swiper').find('li').size() > 1) {
    new Swiper('.fresh-list-swiper', {
        lazyLoading: true,
        lazyLoadingInPrevNext: true,
        grabCursor: true,
        slidesPerView: 'auto',
        slideElement: 'li',
        watchSlidesVisibility: true
    });
}

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

// logo动画
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, 60 * 1000);
        } else {
            window.requestAnimationFrame(tsAnimate);
        }
    }
}

setTimeout(tsAnimate, 3000);

$('.home-header .iconfont').on('touchstart', function() {
    $(this).addClass('highlight');
}).on('touchend touchcancel', function() {
    $(this).removeClass('highlight');
});

// 底部留出tab 的高度
window.reMarginFooter('.footer-tab');

// 原有项目 _Channel 写错位置,这里清理掉
window.setCookie('_Channel', $mobileWrap.data('channel'), {
    expires: -1,
    domain: '.m.yohobuy.com'
});

// 记录当前 _Channel
window.setCookie('_Channel', $mobileWrap.data('channel'), {
    expires: 30,
    domain: '.yohobuy.com'
});

// 店铺推荐滑动效果

var shopSwiper = new Swiper('.shop-recom-swiper-container', {
    centeredSlides: true,
    slidesPerView: 'auto',
    paginationClickable: true,
    slideActiveClass: 'recom-active',
});

// 店铺推荐人数和收藏初始查询
var checkShop = function() {
    var shopIds = [];
    $('.recom-shop').each(function() {
        shopIds.push($(this).attr('shopId'));
    });
    $.ajax({
        type: 'POST',
        url: '/channel/shopRecom',
        data: {
            shopIds: shopIds.join(','),
        },
    }).then(function(result) {
        if (result.code === 200) {
            for (var i = 0, elem;
                (elem = result.data[i]) != null; i++) {
                var a = $('.recom-shop[shopId = ' + elem.id + ']');
                a.find('.faved-num').text(elem.collectionNum + '人已收藏');
                if (elem.favorite) {
                    a.find('.fav-no').hide();
                    a.find('.fav-yes').show();
                }
            }
        }
    });
};
checkShop();

// 店铺收藏 || 取消收藏
var $collect = $('.fav-container');
$collect.on('touchstart', function() {
    var opt = $(this).hasClass('fav-yes') ? 'cancel' : 'ok';
    var self = $(this);
    var num = parseInt(self.parent().find('.faved-num').html());

    $.ajax({
        method: 'get',
        url: location.protocol + '//m.yohobuy.com' + '/product/opt/favoriteBrand',
        data: {
            id: self.parent().attr('shopId'),
            opt: opt,
            type: 'shop',
        },
        xhrFields: {
            withCredentials: true
        },
        success: function(data) {
            var url = '';

            if (data.code === 200) {
                if (self.hasClass('fav-yes')) {
                    self.hide();
                    self.prev().show();
                    num--;
                    self.parent().find('.faved-num').html(num + '人已收藏');
                    tip.show('取消收藏成功!');
                } else {
                    self.hide();
                    self.next().show();
                    num++;
                    self.parent().find('.faved-num').html(num + '人已收藏');
                    tip.show('收藏成功!');
                }
            }

            if (data.code === 400) {
                url = data.data;
                location.href = url;
            }

        },
        error: function() {
            tip.show('网络断开连接了~');
            searching = false;
        }
    });
});

// vip专属等级查询
$.ajax({
    method: 'POST',
    url: location.protocol + '//m.yohobuy.com' + '/channel/userVip',
    data: {
        channel: 1,
    },
    success: function(data) {
        var vip = $('.vip-only');
        if (data.code === 200 && Number(data.current_vip_level) !== 0) {
            vip.show();
            vip.find('.vip' + data.current_vip_level).show();
        }
    },
    error: function() {
        tip.show('网络断开连接了~');
        searching = false;
    }
});

// sale倒计时
var saleTime = function(elem, offsetTime) {
    var hour = parseInt(offsetTime / (60 * 60), 10),
        minute = parseInt(offsetTime % (60 * 60) / 60, 10),
        second = offsetTime % 60;

    if (offsetTime >= -1) {
        $(elem).find('.hour').text(hour < 0 ? '00' : (hour < 10 ? ('0' + hour) : hour));
        $(elem).find('.minute').text(minute < 0 ? '00' : (minute < 10 ? ('0' + minute) : minute));
        $(elem).find('.second').text(second < 0 ? '00' : (second < 10 ? ('0' + second) : second));

        if (offsetTime <= -1) { // 结束倒计时刷新状态
            console.log('time done!');
        } else {
            setTimeout(function() {
                saleTime(elem, --offsetTime);
            }, 1000);
        }
    }

};
let endTime = $('.sale-floor-time').attr('data-time'),

    // ~~两次取反位运算就是取整
    limit = ~~((endTime * 1000 - Date.now()) / 1000);
saleTime('.sale-floor-time', limit);

require('./maybe-like')();

C_ID = window._ChannelVary[window.cookie('_Channel')];

$('.search-btn').click(function() {
    if (window._yas && window._yas.sendCustomInfo) {
        window._yas.sendCustomInfo({
            op: 'YB_HOME_SEARCH_C',
            param: JSON.stringify({
                C_ID: C_ID,
            })
        }, true);
    }
});