saunter.js
14.3 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
* '逛'js
* @author: yue.liu@yoho.cn
* @date;2015/3/31
*/
var $ = require('jquery'),
IScroll = require('iscroll/iscroll-probe'),
ellipsis = require('mlellipsis');
require('lazyload');
/**
* 初始化Android页面
*/
exports.initAndroid = function() {
$(function() {
var $loginTip = $('#login-tip'),
winH,
winW,
tipH,
tipW;
var isLogin = $('#is-login').val();
isLogin = isLogin ? isLogin : 'N';
//头部作者信息样式计算(在描述信息过长时换行显示, 去除intro的padding-top)
var $linkC = $('#link-container'),
$avatar = $linkC.find('.avatar'),
$name = $linkC.find('.name'),
$intro = $linkC.find('.intro');
if ($avatar.outerWidth(true) + $name.outerWidth(true) + $intro.outerWidth(true) > $(window).width()) {
$intro.css('padding-top', 0);
}
//类型3文章相关变量
var isInit = true,
atContainer,
thumbContainer,
prodList,
arrowPosition;
//样式初始化
$('#wrapper').removeClass('no-android');
//相关文章截取文字
ellipsis.init();
$('.post-list').find('span').each(function() {
this.mlellipsis(2);
});
$('.brand-name').each(function() {
this.mlellipsis(1);
});
//图片懒加载
$('img.lazy').lazyload();
$('.thumb-container img.lazy').lazyload({
event: 'load'
});
//提示信息位置
winH = $(window).height();
winW = $(window).width();
tipH = $loginTip.height();
tipW = $loginTip.width();
$loginTip.css({
top: (winH - tipH) / 2,
left: (winW - tipW) / 2
});
//绑定商品信息的收藏和取消收藏事件(相关推荐和搭配模块)
$('.good-list, .prod-list').delegate('.good-islike', 'touchstart', function(e) {
var $cur,
$good,
id;
if (isLogin === 'N') {
$cur = $(e.currentTarget);
$good = $cur.closest('.good-info');
id = $good.data('id');
$.ajax({
type: 'GET',
url: '/favorite/product',
data: {
st: 1,
product_skn: id
}
}).then(function(data) {
if (data.code === 200) {
$cur.toggleClass('good-like');
} else if (data.code === 400) {
//提示登录信息
$('#login-tip').fadeIn(500, function() {
setTimeout(function() {
$('#login-tip').fadeOut(500);
}, 1000);
});
}
});
}
}).delegate('.good-islike', 'click', function(e) {
if (isLogin === 'Y') {
e.preventDefault();
}
});
//类型三文章
atContainer = $('.article-type-three');
if (atContainer.length === 0) {
return;
}
thumbContainer = atContainer.find('.thumb-container');
prodList = atContainer.find('.prod-list');
arrowPosition = function(element, index) {
//640为设计图尺寸,也就是font-size为40下的1rem,所以数值都参照640定
var container = element,
item = container.find('li'),
len = 5, //获取一共多少li
midNum = Math.ceil(len / 2), //获取中间li的index+1
documentFont = parseInt($("html").css("fontSize")), //获取html的font-size
itemMarginRight = parseInt(item.css('marginRight')) / documentFont * 40, //获取li的margin-right在640下数值
itemWidth = item.width() / documentFont * 40, //获取li的widtht在640下数值
surPlusValue = (parseInt(container.css('padding-left')) / documentFont * 40 - itemMarginRight) / 2, //获取container的padding-left与itemMarginRight相减后除以2在640下数值
backgroundLeft; //container背景图片左偏移的位置
/**
* 320为640分辨率下中间值
* index-midNum为偏移中间li的数值
* (index - midNum) * (itemWidth + itemMarginRight)当前li偏移中间li的距离
* - surPlusValue 减去container的padding与li的margin-right差值的一半
* 除以40,由640下的px变为rem
*/
backgroundLeft = -(320 - (index - midNum) * (itemWidth + itemMarginRight) - surPlusValue) / 40 + 'rem';
container.css({
"backgroundPosition": backgroundLeft + " bottom"
});
};
//点击分类,显示分类下推荐商品列表
thumbContainer.delegate('.thumb', 'click', function() {
var curItem,
index;
curItem = $(this);
index = curItem.index();
thumbContainer.find('.thumb').removeClass('focus')
curItem.addClass('focus');
arrowPosition(thumbContainer, index + 1);
prodList.find('.prod')
.addClass('hide')
.eq(index)
.removeClass('hide');
//图片懒加载
$("img.lazy").lazyload();
//scroll to top
if (!isInit) {
$('body').animate({
scrollTop: atContainer.offset().top
}, 400);
} else {
isInit = false;
}
});
//默认选中第一个
thumbContainer.find('.thumb:first-child').click();
});
};
/**
* 初始化非Android页面
*/
exports.initOther = function() {
var hasAt = true, //是否包含article-type-three
atContainer,
thumbContainer,
fixedThumbContainer = $(''), //初始化为jq对象
prodList,
winH,
myScroll;
$(function() {
var $loginTip = $('#login-tip'),
winW,
tipH,
tipW;
var isLogin = $('#is-login').val();
isLogin = isLogin ? isLogin : 'N';
//article-type-three相关变量
var isInit = true,
scrollToEl = document.querySelector('#wrapper .article-type-three'),
thumbs,
arrowPosition;
//头部作者信息样式计算(在描述信息过长时换行显示, 去除intro的padding-top)
var $linkC = $('#link-container'),
$avatar = $linkC.find('.avatar'),
$name = $linkC.find('.name'),
$intro = $linkC.find('.intro');
//
if ($avatar.outerWidth(true) + $name.outerWidth(true) + $intro.outerWidth(true) > $(window).width()) {
$intro.css('padding-top', 0);
}
//相关文章截取文字
ellipsis.init();
$('.post-list').find('span').each(function() {
this.mlellipsis(2);
});
$('.brand-name').each(function() {
this.mlellipsis(1);
});
//提示信息位置
winH = $(window).height();
winW = $(window).width();
tipH = $loginTip.height();
tipW = $loginTip.width();
$loginTip.css({
top: (winH - tipH) / 2,
left: (winW - tipW) / 2
});
//绑定商品信息的收藏和取消收藏事件(相关推荐和搭配模块)
$('.good-list, .prod-list').delegate('.good-islike', 'touchstart', function(e) {
var $cur,
$good,
id;
if (isLogin === 'N') {
$cur = $(e.currentTarget);
$good = $cur.closest('.good-info');
id = $good.data('id');
$.ajax({
type: 'GET',
url: '/favorite/product',
data: {
st: 1,
product_skn: id
}
}).then(function(data) {
if (data.code === 200) {
$cur.toggleClass('good-like');
} else if (data.code === 400) {
//提示登录信息
$('#login-tip').fadeIn(500, function() {
setTimeout(function() {
$('#login-tip').fadeOut(500);
}, 1000);
});
}
});
}
}).delegate('.good-islike', 'click', function(e) {
if (isLogin === 'Y') {
e.preventDefault();
}
});
//article-three效果
atContainer = $('.article-type-three');
if (atContainer.length > 0) {
hasAt= true;
} else {
hasAt = false;
}
//初始化
thumbContainer = atContainer.find('.thumb-container');
prodList = atContainer.find('.prod-list');
//
fixedThumbContainer = $('#wrapper')
.after(thumbContainer.clone().addClass('fixed-thumb-container fixed-bottom hide'))
.next('.thumb-container');
thumbs = $('.thumb');
//图片懒加载
$("img.lazy").lazyload({
event: 'load'
});
/**
* 计算小箭头位置函数
* @param index(类型为number,调用函数li的index)
*/
arrowPosition = function(element, index) {
//640为设计图尺寸,也就是font-size为40下的1rem,所以数值都参照640定
var container = element,
item = container.find('li'),
len = 5, //获取一共多少li
midNum = Math.ceil(len / 2), //获取中间li的index+1
documentFont = parseInt($("html").css("fontSize")), //获取html的font-size
itemMarginRight = parseInt(item.css('marginRight')) / documentFont * 40, //获取li的margin-right在640下数值
itemWidth = item.width() / documentFont * 40, //获取li的widtht在640下数值
surPlusValue = (parseInt(container.css('padding-left')) / documentFont * 40 - itemMarginRight) / 2, //获取container的padding-left与itemMarginRight相减后除以2在640下数值
backgroundLeft; //container背景图片左偏移的位置
/**
* 320为640分辨率下中间值
* index-midNum为偏移中间li的数值
* (index - midNum) * (itemWidth + itemMarginRight)当前li偏移中间li的距离
* - surPlusValue 减去container的padding与li的margin-right差值的一半
* 除以40,由640下的px变为rem
*/
backgroundLeft = -(320 - (index - midNum) * (itemWidth + itemMarginRight) - surPlusValue) / 40 + 'rem';
container.css({
"backgroundPosition": backgroundLeft + " bottom"
});
};
/**
* 分类的点击事件句柄
*/
function thumbClickEvt(e) {
var that = $(e.currentTarget),
index = that.index(),
other;
if (that.closest('.fixed-thumb-container').length > 0) {
other = thumbContainer.find('.thumb:eq(' + index + ')');
} else {
other = fixedThumbContainer.find('.thumb:eq(' + index + ')');
}
//set status of that & other synchronous
thumbs.removeClass('focus');
that.addClass('focus');
other.addClass('focus');
arrowPosition(thumbContainer, index + 1);
arrowPosition(fixedThumbContainer, index + 1);
prodList.find('.prod')
.addClass('hide')
.eq(index)
.removeClass('hide');
if (!isInit) {
if (myScroll) {
myScroll.scrollToElement(scrollToEl, 400);
}
} else {
isInit = false;
}
}
thumbs.click(thumbClickEvt);
//默认选中第一个
thumbContainer.find('.thumb:first-child').click();
});
/**
* init iscroll
*/
window.onload = function() {
var tContainerH, //thumber-container高度
containerH, //article-type-three高度
containerTop; //article-type-three offset top
myScroll = new IScroll('#wrapper', {
probeType: 3,
mouseWheel: true,
click: true
});
document.addEventListener('touchmove', function (e) {
e.preventDefault();
}, false);
if (!hasAt) {
return;
}
tContainerH = thumbContainer.outerHeight();
containerH = atContainer.height();
containerTop = atContainer.offset().top;
/**
* scroll前重置container状态
*/
function resetStatus() {
fixedThumbContainer.removeClass('fixed-top fixed-bottom absolute hide').css('top', '');
}
myScroll.on('scroll', function() {
var sTop = -this.y;
resetStatus();
if (sTop <= containerTop - winH + tContainerH) {
fixedThumbContainer
.addClass('fixed-bottom');
} else if (sTop <= containerTop) {
fixedThumbContainer
.addClass('hide');
} else if (sTop <= containerTop + containerH - tContainerH) {
fixedThumbContainer
.addClass('fixed-top');
} else if (sTop <= containerTop + containerH) {
fixedThumbContainer
.addClass('absolute')
.css({
top: containerTop + containerH - tContainerH - sTop
});
} else if (sTop > containerTop + containerH) {
fixedThumbContainer
.addClass('hide');
}
});
//手动触发scroll, 初始化fixedThumb的位置和显示
myScroll.scrollTo(0, 0, 0.1);
};
};