recommend-for-you-product-desc.js 1.46 KB
/**
 * 为您优选
 * @author: liangzhifeng<zhifeng.liang@yoho.cn>
 * @date: 2015/11/16
 */


var Swiper = require('yoho.iswiper'),
    $ = require('jquery');

var recommendSwiper,
    $recommendForYou = $('.recommend-for-you'),
    preferenceUrl = $('#preferenceUrl').val(),
    winH = $(window).height(),
    end = false,
    requesting = false;


function request() {
    if (requesting || end) {
        return;
    }

    requesting = true;

    if (preferenceUrl) {
        $.get(preferenceUrl).then(function(html) {
            if (html.length < 5) {
                $recommendForYou.css('display', 'none');
            } else {
                $recommendForYou.html(html).show();
                if ($('#swiper-recommend').length) {
                    recommendSwiper = new Swiper('#swiper-recommend', {
                        slidesPerView: 'auto',
                        grabCursor: true,
                        slideElement: 'a',
                        lazyLoading: true,
                        watchSlidesVisibility: true
                    });
                }
            }

            requesting = false;
            end = true;

            window.rePosFooter();

        }).fail(function() {
            $recommendForYou.hide();
        });
    }
}

function scrollHandler() {
    if (!end || $(window).scrollTop() + winH >= $(document).height() - 200) {
        request();
    }
}

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