Blame view

static/js/home/maybe-like.js 3.83 KB
梁志锋 authored
1 2
/**
 * “你可能喜欢”模块JS
梁志锋 authored
3 4
 * @author: liangzhifeng<zhifeng.liang@yoho.cn>
 * @date: 2015/10/12
梁志锋 authored
5 6
 */
xuqi authored
7
var $ = require('jquery'),
xuqi authored
8
    Hammer = require('yoho.hammer'),
梁志锋 authored
9
    tip = require('../plugin/tip'),
梁志锋 authored
10
    loading = require('../plugin/loading'),
xuqi authored
11
    lazyLoad = require('yoho.lazyload');
梁志锋 authored
12
梁志锋 authored
13 14
var navHammer,
    winH = $(window).height(),
梁志锋 authored
15
    $goodList = $('#goods-list'),
梁志锋 authored
16
    searching = false,
梁志锋 authored
17
    page = 0,
梁志锋 authored
18
    gender = null,
梁志锋 authored
19
    kidsType = $('.mobile-wrap').hasClass('kids-wrap') ? true : false,
梁志锋 authored
20
    lifestyleType = $('.mobile-wrap').hasClass('lifestyle-wrap') ? true : false,
梁志锋 authored
21
    num,
梁志锋 authored
22
    url;
梁志锋 authored
23
梁志锋 authored
24
var $curNav,
梁志锋 authored
25 26 27
    index,
    $navList = $('#maybe-like-nav');
梁志锋 authored
28
//ajax url
梁志锋 authored
29
if (kidsType) {
梁志锋 authored
30
    url = '/product/recom/maylikekids';
梁志锋 authored
31 32
} else if (lifestyleType) {
    url = '/product/recom/maylikelife';
梁志锋 authored
33
} else {
梁志锋 authored
34
    gender = $('.mobile-wrap').hasClass('boys-wrap') ? '1,3' : '2,3',
梁志锋 authored
35 36 37
    url = '/product/recom/maylike?gender=' + gender;
}
梁志锋 authored
38 39
$curNav = $navList.children('.focus');
梁志锋 authored
40
if (lifestyleType) {
梁志锋 authored
41 42 43 44 45
    navHammer = new Hammer($navList[0]);
    navHammer.on('tap', function(e) {
        var $this = $(e.target).closest('li'),
            $goods = $('.goods-list'),
            $content;
梁志锋 authored
46
梁志锋 authored
47
        e.preventDefault();
梁志锋 authored
48 49 50
        if ($this.hasClass('focus')) {
            return;
        }
梁志锋 authored
51
梁志锋 authored
52
        index = $this.index();
梁志锋 authored
53
梁志锋 authored
54 55
        $this.addClass('focus');
        $curNav.removeClass('focus');
梁志锋 authored
56
梁志锋 authored
57 58 59
        $goods.not('.hide').addClass('hide');
        $content = $goods.eq(index);
        $content.removeClass('hide');
梁志锋 authored
60
梁志锋 authored
61 62 63
        $curNav = $this;

        $(document).trigger('scroll'); //Trigger lazyLoad
梁志锋 authored
64
        e.srcEvent.stopPropagation();
梁志锋 authored
65 66
    });
}
梁志锋 authored
67
梁志锋 authored
68
loading.init($('.maybe-like'));
梁志锋 authored
69
梁志锋 authored
70
function search() {
梁志锋 authored
71
    if (searching) {
梁志锋 authored
72 73
        return;
    }
梁志锋 authored
74 75
    searching = true;
梁志锋 authored
76
    loading.showLoadingMask();
梁志锋 authored
77
梁志锋 authored
78 79 80 81 82 83 84 85 86
    //num = $goodList.find('.good-info').length;
    $.ajax({
        type: 'GET',
        url: url,
        data: {
            page: page + 1
        },
        success: function(data) {
            if (data === ' ') {
梁志锋 authored
87
                searching = false;
梁志锋 authored
88
                loading.hideLoadingMask();
梁志锋 authored
89 90 91 92 93
                if (gender) {
                    if (gender === '1,3') {
                        url = '/boys/bottomBanner';
                    } else {
                        url = '/girls/bottomBanner';
梁志锋 authored
94
                    }
梁志锋 authored
95 96 97 98
                    $.ajax({
                        type: 'GET',
                        url: url,
                        success: function(data) {
梁志锋 authored
99
                            if (data && data.img) {
梁志锋 authored
100 101 102 103 104 105 106 107
                                $('#load-more-img').show();
                                $('#load-more-img a').attr('href', data.url);
                                $('#load-more-img a > img').attr('src', data.img);
                            }
                        },
                        error: function() {
                        }
                    });
梁志锋 authored
108
梁志锋 authored
109 110 111
                }
                return;
            }
梁志锋 authored
112
梁志锋 authored
113
            num = $goodList.find('.good-info').length;
114
梁志锋 authored
115
            $goodList.append(data);
梁志锋 authored
116
梁志锋 authored
117 118 119 120 121
            // 2015/10/31 fei.hong: 修复第一页分页不显示图片的问题
            if (num === 0) {
                lazyLoad($goodList.find('.good-info').find('img.lazy'));
            } else {
                lazyLoad($goodList.find('.good-info:gt(' + (num - 1) + ')').find('img.lazy'));
梁志锋 authored
122 123
            }
梁志锋 authored
124
            searching = false;
梁志锋 authored
125
            loading.hideLoadingMask();
梁志锋 authored
126 127 128 129
            page++;
        },
        error: function() {
            tip.show('网络断开连接了~');
梁志锋 authored
130
            searching = false;
梁志锋 authored
131
            loading.hideLoadingMask();
梁志锋 authored
132 133
        }
    });
梁志锋 authored
134 135
}
136
$('.maybe-like p').on('touchstart', function(e) {
梁志锋 authored
137 138 139
    search();
});
140
function scrollHandler() {
141
    if ($(window).scrollTop() + winH >= $(document).height() - 200) {
梁志锋 authored
142 143
        search();
    }
144 145 146 147 148
}

//srcoll to load more
$(window).scroll(function() {
    window.requestAnimationFrame(scrollHandler);
梁志锋 authored
149
});