maybe-like.js
3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* “你可能喜欢”模块JS
* @author: liangzhifeng<zhifeng.liang@yoho.cn>
* @date: 2015/10/12
*/
var $ = require('jquery'),
tip = require('../plugin/tip'),
lazyLoad = require('yoho.lazyload');
var winH = $(window).height(),
loadMoreH = $('#load-more').height(),
$goodList = $('#goods-list'),
loading = false,
page = 0,
gender = null,
kidsType = $('.mobile-wrap').hasClass('kids-wrap') ? true : false,
lifestyleType = $('.mobile-wrap').hasClass('lifestyle-wrap') ? true : false,
num,
url;
var $curNav,
index,
$navList = $('#maybe-like-nav');
//ajax url
if (kidsType) {
url = '/product/recom/maylikekids';
} else if (lifestyleType) {
url = '/product/recom/maylikelife';
} else {
gender = $('.mobile-wrap').hasClass('boys-wrap') ? '1,3' : '2,3',
url = '/product/recom/maylike?gender=' + gender;
}
$curNav = $navList.children('.focus');
$('#maybe-like-nav').delegate('li', 'tap', function() {
var $this = $(this),
$goods = $('.goods-list'),
$content;
if ($this.hasClass('focus')) {
return;
}
index = $this.index();
$this.addClass('focus');
$curNav.removeClass('focus');
$goods.not('.hide').addClass('hide');
$content = $goods.eq(index);
$content.removeClass('hide');
$curNav = $this;
$(document).trigger('scroll'); //Trigger lazyLoad
});
//srcoll to load more
$(window).scroll(function () {
if ($(window).scrollTop() + winH >= $(document).height() - loadMoreH) {
if (loading) {
return;
}
loading = true;
num = $goodList.children('.good-info').length;
$.ajax({
type: 'GET',
url: url,
data: {
page: page + 1
},
success: function(data) {
if (data === ' ') {
loading = true;
if (gender) {
if (gender === '1,3') {
url = '/boys/bottomBanner';
} else {
url = '/girls/bottomBanner';
}
$.ajax({
type: 'GET',
url: url,
success: function(data) {
if (data && data[0] && data[0].banner[0]) {
$('#load-more-img').show();
$('#load-more-img a').attr('url', data[0].banner[0].url);
$('#load-more-img a > img').attr('src', data[0].banner[0].img);
}
},
error: function() {
}
});
}
return;
}
$goodList.append(data);
num = $goodList.find('.good-info').length;
//lazyLoad
lazyLoad($goodList.children('.good-info:gt(' + (num - 1) + ')').find('img.lazy'));
loading = false;
page++;
},
error: function() {
tip.show('网络断开连接了~');
loading = false;
}
});
}
});