hotrank.js
6.21 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* 首页
* @author: liuyue<yue.liu@yoho.cn>
* @date: 2015/12/17
*/
var $ = require('yoho.jquery'),
Handlebars = require('yoho.handlebars'),
lazyLoad = require('yoho.lazyload');
require('../common/slider');
require('../common/logo-brand');
lazyLoad($('img.lazy'));
$('.slide-container').slider();
$('.logo-brand').logoBrand({
showNum: 10,
url: $('.logo-brand').data('url')
});
/*
* 一周热卖
*/
(function($) {
var floatlayer = $('.hot-week').find('.floatlayer'),
sid = $('.hot-cate').find('li').eq(0).data('sid'),
weekOffsetTop,
source,
template,
page = 1,
hotCateW = 0,
weekEnd = false;
source = '\{{# list}}' +
'<div class="good-info">' +
'<div class="item-img">' +
'<a class="good-thumb" target="_blank" href="\{{url}}">' +
'<img class="lazy" data-original="\{{thumb}}">' +
'</a>' +
'</div>' +
'<div class="good-detail-text">' +
'<a target="_blank" href="{{url}}">{{name}}</a>' +
'<p class="price">' +
'{{# marketPrice}}<span class="market-price">¥{{.}}</span>{{/ marketPrice}} ' +
'<span class="sale-price{{#unless marketPrice}}prime-cost{{/unless}}">' +
'¥{{salePrice}}</span>' +
'</p>' +
'</div>' +
'</div>' +
'{{/ list}}';
//监听滚动事件,控制浮层样式及下拉加载更多
$(window).on('scroll', function() {
var maxH = weekOffsetTop + $('.hot-week').outerHeight(),
sid;
//热卖右侧浮动导航位置
weekOffsetTop = $('.hot-week').offset().top;
if ($(this).scrollTop() >= weekOffsetTop - $(window).height() / 2) {
if (floatlayer.offset().top + floatlayer.outerHeight() >= maxH) {
floatlayer.css({
position: 'absolute',
marginTop: 0,
top: $('.hot-week').outerHeight() - floatlayer.outerHeight()
});
} else {
floatlayer.css({
marginTop: -floatlayer.height() / 2
}).stop().animate({
opacity: 1
}, 200, function() {
floatlayer.show();
});
}
} else {
floatlayer.stop().animate({
opacity: 0
}, 200, function() {
floatlayer.hide();
});
}
if (floatlayer.offset().top >= $(this).scrollTop() + ($(window).height() - floatlayer.outerHeight()) / 2) {
floatlayer.css({
position: 'fixed',
marginTop: -floatlayer.height() / 2,
top: '50%'
});
}
//下拉加载
if ($(this).scrollTop() >= weekOffsetTop + $('.hot-week').height() - $(window).height()) {
if (!weekEnd) {
sid = $('.hot-cate').find('li.current').data('sid');
page++;
weekEnd = true; //防止多次请求
weekAjax(sid, page);
}
}
});
//热卖横导航及竖导航的超出显示控制
$('.hot-cate').find('li').each(function(i) {
var maxW = $('.hot-cate').width();
if (hotCateW <= maxW) {
hotCateW = hotCateW + $(this).width();
if (hotCateW >= maxW) {
$('.floatlayer').find('li').eq(i).hide().nextAll().hide();
}
}
});
//热卖横导航点击事件处理
$('.hot-cate').on('click', 'li', function() {
var nowIndex = $(this).index(),
sid = $(this).data('sid');
if ($(this).hasClass('current')) {
return;
}
//处理current样式
$(this).addClass('current').siblings().removeClass('current');
$('.floatlayer').find('li').removeClass('current').eq(nowIndex).addClass('current');
//返回热卖顶部,110为floor-header所占高度
$('body,html').stop().animate({
scrollTop: weekOffsetTop - 110
}, 500);
//调用ajax请求函数,重置page,weekEnd
weekAjax(sid);
page = 1;
});
//热卖右侧悬浮导航点击事件处理
$('.floatlayer').on('click', 'li', function() {
var nowIndex = $(this).index();
if ($(this).hasClass('current')) {
return;
}
$('.hot-cate').find('li').eq(nowIndex).trigger('click');
//处理current样式
$(this).addClass('current').siblings().removeClass('current');
$('.hot-cate').find('li').removeClass('current').eq(nowIndex).addClass('current');
});
/*
* 热卖内容ajax请求
* param: sid(ajax请求的id), page(ajax请求的页码)
*/
function weekAjax(sid, page) {
var param = {};
if (page) {
param = {
sid: sid,
page: page
};
} else {
param = {
sid: sid
};
}
$.ajax({
type: 'GET',
dataType: 'json',
url: '/product/index/getdata',
data: param,
success: function(res) {
var data;
if (res.code === 200) {
data = {
list: res.data
};
//mustache渲染数据,并替换原内容
template = Handlebars.compile(source);
if (page) {
$('.hot-week-list').append(template(data));
floatlayer.css({
position: 'fixed',
marginTop: -floatlayer.height() / 2,
top: '50%'
});
} else {
$('.hot-week-list').html(template(data));
}
lazyLoad($('img.lazy'));
weekEnd = false;
}
}
});
}
//热卖中导航current类处理
$('.hot-cate').find('li').eq(0).addClass('current');
$('.floatlayer').find('li').eq(0).addClass('current');
})($);