best-for-you.js 1.42 KB
var $ = require('yoho-jquery');

var bestForYouPage = {},
    $bestForYou = $('.best-for-you');

var bestForYouHbs = require('hbs/common/best-for-you.hbs');

bestForYouPage = {
    init: function() {
        if (!$bestForYou.length) {
            return;
        }

        this.getRecommend();
    },
    getRecommend: function() {
        var _this = this;

        $.ajax({
            url: '/common/getRecommend',
            type: 'POST',
            dataType: 'json',
            data: {
                recPos: 100004,
                limit: 15
            },
            success: function(d) {
                $bestForYou.html(bestForYouHbs({bestForYou: d}));
                _this.changeNext();
            }
        });
    },

    // 换一批按钮
    changeNext: function() {
        var $box, boxInfo;

        $box = $bestForYou.children('.product-wrap');

        boxInfo = {
            index: 0,
            width: $box.find('li').width() + 10,
            totalPage: Math.ceil(($box.find('li').length + 1) / 5)
        };

        if (boxInfo.totalPage > 1) {
            boxInfo.totalPage = boxInfo.totalPage > 3 ? 3 : boxInfo.totalPage;
            $bestForYou.find('.change-btn').show().click(function() {
                boxInfo.index++;
                $box.get(0).scrollLeft = (boxInfo.index % boxInfo.totalPage) * boxInfo.width * 5;
            });
        }
    }
};

$(document).ready(function() {
    bestForYouPage.init();
});