hot-rank.page.js 3.82 KB
let $ = require('yoho-jquery'),
    Swiper = require('yoho-swiper'),
    lazyLoad = require('yoho-jquery-lazyload'),
    loading = require('js/plugin/loading'),
    qs = require('yoho-qs');

let page = 1,
    winH,
    listTop,
    notab = 0,
    sort = '',
    id = '',
    hotrankNav,
    noResult = '<p class="no-result">未找到相关搜索结果</p>';

require('js/common/suspend-cart');
require('js/common');

let share = require('js/common/share');

share({
    title: '有货【会员日】开启啦!9月28日潮集狂欢趴等你来HI!限时6大福利,快参与起来喽~',
    link: location.href,
    desc: 'YOHO!BUY',
    imgUrl: 'http://static.yohobuy.com/m/v1/img/touch/apple-touch-icon-144x144-precomposed-new.png'

});

function hotrank(pageIndex, sortIndex, tabId, noTab) {
    loading.showLoadingMask();
    $.ajax({
        type: 'GET',
        url: '/product/newsale/selectHotrank?' + sortIndex,
        dataType: 'html',
        data: {
            page: pageIndex,
            tab_id: tabId,
            notab: noTab,
            gender: qs.gender
        },
        success: function(data) {
            if (data === ' ') {
                if ($('.rank-main').length < 1 && $('.goods-nav').length < 1) {
                    $('#hotRank').html(noResult);
                } else {
                    if (pageIndex === 1) {
                        hotrankNav = $('.goods-nav').prop('outerHTML');

                        $('#hotRank').html(hotrankNav + noResult);
                    }
                }
            } else {
                $('.no-result').remove();
                if (pageIndex === 1) {
                    $('.rank-main').remove();
                }
                $('#hotRank').append(data);
            }

            lazyLoad($('img.lazy'));
            $('.rank-main ul li:gt(9)').find('.item-content i').removeClass('top');
            winH = $(window).height();
            if ($('.rank-main').length !== 0) {
                $('#yoho-footer').css('position', 'static');
                listTop = $('.rank-main').find('ul').offset().top;
            } else {
                $('#yoho-footer').css({
                    position: 'fixed',
                    width: '100%'
                });
            }
            new Swiper('.s-goods-nav', {
                grabCursor: true,
                slidesPerView: 'auto',
                slideElement: 'li'
            });
            loading.hideLoadingMask();

            window.rePosFooter && window.rePosFooter();
        }
    });
}

function scrollHandler() {
    if (page === 2) {
        return;
    }
    if ($(window).scrollTop() + winH < listTop + $('#hotRank').height() - 100) {
        return;
    }
    page = 2;
    notab = 1;
    hotrank(page, sort, id, notab);
}

$(window).scroll(function() {
    window.requestAnimationFrame(scrollHandler);
});

hotrank(page, sort, id, notab);
$('#hotRank').on('touchend touchcancel', function() {
    let ev = window.event;
    let target = ev.target || ev.srcElement;

    if (target.nodeName.toLowerCase() === 'span') {
        $('.s-goods-nav .nav-item').removeClass('active');
        target.parentNode.className = 'active ' + target.parentNode.className;
        id = target.getAttribute('data-id') ? target.getAttribute('data-id') : '';
        sort = target.getAttribute('data-sort') ? target.getAttribute('data-sort') : '';
        page = 1;
        notab = 1;
        hotrank(page, sort, id, notab);
    }
});
$('#hotRank').bind('contextmenu', function() {
    return false;
});
$('#hotRank').on('touchstart', function() {
    let ev = window.event;
    let target = ev.target || ev.srcElement;

    if (target.nodeName.toLowerCase() === 'span') {
        target.parentNode.className = 'bg-active ' + target.parentNode.className;
    }
}).on('touchend touchcancel', function() {
    $('.s-goods-nav .nav-item').removeClass('bg-active');
});