maybe-like.js 1.48 KB
/**
 * “你可能喜欢”模块JS
 * @author: liangzhifeng<zhifeng.liang@yoho.cn>
 * @date: 2015/10/12
 */

var $ = require('yoho.zepto'),
    lazyLoad = require('yoho.lazyload'),
    tip = require('../plugin/tip');

var winH = $(window).height(),
    loadMoreH = $('#load-more').height(),
    loading = false,
    end = false,
    page = 0,
    gender = '',
    num,
    res;

gender = $('.mobile-wrap').hasClass('boys-wrap') ? '1,3' : '2,3';

var $goodList = $('#goods-list');

//srcoll to load more
$(window).scroll(function () {
    if ($(window).scrollTop() + winH >= $(document).height() - loadMoreH) {
        loading = true;
        num = $goodList.children('.good-info').length;
        $.ajax({
            type: 'GET',
            url: '/product/recom/maylike?gender='+gender,
            data: {
                page: page + 1
            },
            success: function(data) {
                if (data.code === 200) {
                    res = data.data;

                    if (res.end) {
                        end = res.end;
                    }

                    $goodList.append(res.product);

                    //lazyLoad
                    lazyLoad($goodList.children('.good-info:gt(' + (num - 1) + ')').find('img.lazy'));

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

    
});