brand.js
5.96 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
var $ = require('yoho.jquery'),
Swiper = require('yoho.iswiper'),
Mustache = require('yoho.mustache'),
FetchBrand = require('./fetchbrand'),
jgestures = require('jgestures');
var fetching = false,
pagecount = 10,
page = window.global_totalcount >= pagecount ? pagecount : window.global_totalcount,
loaded = {},
slideCache = [],
slideMap = {},
lastScrollY = window.pageYOffset,
scrollY = window.pageYOffset,
innerHeight,
topViewPort,
bottomViewPort;
/**
*获得模板
*/
var template;
var parenttop = 145 * pagecount;
window.slideCache1 = [];
window.slideCache2 = [];
window.slideCache3 = [];
function showLoadPreview() {
document.querySelector('.loadmore').style.display = 'block';
}
function hideLoadPreview() {
document.querySelector('.loadmore').style.display = 'none';
}
/**
* 更新节点缓存
* @param {[type]} node [description]
* @return {[type]} [description]
*/
function updateSlideCache(node) {
var list = node.querySelectorAll('.infislide');
var len = list.length;
var obj;
for (var i = 0; i < len; i++) {
parenttop += list[i].offsetHeight;
list[i].setAttribute('offtop', parenttop);
var tempimglist = list[i].querySelectorAll('.img');
obj = {
node: list[i],
id: list[i].getAttribute('data-id'),
imgs: tempimglist
}
var tempsrclist = [];
for (var j = 0; j < tempimglist.length; j++) {
var srctext = tempimglist[j].getAttribute('data-src');
tempsrclist.push(srctext);
}
obj.srclist = tempsrclist;
window['slideCache' + global_brandcat].push(obj);
};
}
/**
* 检查元素是否可见
* @param {[type]} id [description]
* @return {Boolean} [description]
*
*/
function isVisible(id) {
var offTop, offsetHeight, data, node;
if (slideMap[id]) {
offTop = slideMap[id].offTop;
offsetHeight = slideMap[id].offsetHeight;
} else {
node = document.getElementById('s-' + id);
offsetHeight = parseInt(node.offsetHeight);
var offtop = node.getAttribute('offtop') || node.offTop;
offTop = parseInt(node.getAttribute('offtop'));
data = {
node: node,
offTop: offTop,
offsetHeight: offsetHeight
};
slideMap[id] = data;
var swiper = node.querySelector('.swiper-container');
var swiperid = swiper.getAttribute('id');
new Swiper('.swiper-container' + swiperid, {
pagination: '.swiper-pagination' + swiperid,
lazyLoading: true,
lazyLoadingInPrevNext: true,
loop: false,
autoplay: false
});
}
if (offTop + offsetHeight > topViewPort && offTop < bottomViewPort) {
return true;
} else {
return false;
}
}
/**
* 更新图片
* @param {[type]} node [description]
* @param {[type]} src [description]
* @return {[type]} [description]
*/
function handler(node, src) {
node.src = src;
node.style.opacity = 1;
loaded[src] = true;
}
/**
* 检测滚动的回调
* @return {[type]} [description]
*/
function handleDefer() {
var list = window['slideCache' + global_brandcat];
for (var i = 0, len = list.length; i < len; i++) {
if (isVisible(list[i].id)) {
var obj = list[i],
imgs = obj.imgs,
srclist = obj.srclist;
for (var j = 0; j < srclist.length; j++) {
var img = new Image();
img.onload = handler(imgs[j], srclist[j]);;
img.src = srclist[j];
}
}
}
}
/**
* 抓取数据
* @return {[type]} [description]
*/
function fetchBrands() {
if (fetching) {
return;
} else {
fetching = true;
}
var senddata = {
brandcat: global_brandcat,
start: page,
count: pagecount
};
var sendparams = {
data: senddata,
url: global_fetchdataurl
};
FetchBrand.FetchBrandDataInfo('brands', sendparams, function(data) {
var frag;
frag = document.createElement('div');
frag.innerHTML = Mustache.render(template, {
brandList: data.data.brandList
});
window.global_totalcount = data.data.total;
document.getElementById(global_showpannel).appendChild(frag);
updateSlideCache(frag);
fetching = false;
handleScroll(null, true);
page += pagecount;
});
}
/**
* 模拟滚动的函数
* @param {[type]} e [description]
* @param {[type]} force [description]
* @return {[type]} [description]
*/
function handleScroll(e, force) {
if (page > window.global_totalcount) {
return;
}
if (!force && lastScrollY === window.scrollY) {
window.setTimeout(handleScroll, 500);
return;
} else {
lastScrollY = window.scrollY;
}
scrollY = window.scrollY;
innerHeight = window.innerHeight; //窗口高度
topViewPort = scrollY - 15000;
bottomViewPort = scrollY + innerHeight + 15000;
if (window.scrollY + innerHeight + 2000 > document.body.offsetHeight) { //是否提前到底
fetchBrands();
}
handleDefer();
window.setTimeout(handleScroll, 500);
}
/**抓取数据**/
function getBrand(catid) {
page = 0;
global_showpannel = 'showpannel' + catid;
global_brandcat = catid;
window.scrollTo(0, 0);
fetchBrands();
}
/**
* 初始化
* @param {[type]} ) { window.setTimeout(handleScroll, 100); fetchBrands();} [description]
* @return {[type]} [description]
*/
$(document).ready(function() {
$.get('/cuxiao/newfestival/brandinfo', function(data) {
template = data;
Mustache.parse(template);
window.setTimeout(handleScroll, 100);
fetchBrands();
$('.navitemwrapper').bind('tapone', function() {
var nowActHeader = $(this),
preActHeader = $('.newfestivalbrandheader').data('actheadtag') || $('.hasunderline'),
tagId = nowActHeader.attr('tagid'),
nowActContent = $('#' + tagId),
preActContent = $('.newfestivalbrandheader').data('actcontenttag') || $('.newfestivalbrandwrapper .show');
preActHeader.removeClass('hasunderline');
nowActHeader.addClass('hasunderline');
$('.newfestivalbrandheader').data('actheadtag', nowActHeader);
preActContent.empty(); //清掉上个界面的数据
parenttop = nowActContent.offset().top; //重置高度
preActContent.removeClass('show').addClass('hide');
nowActContent.removeClass('hide').addClass('show');
$('.newfestivalbrandheader').data('actcontenttag', nowActContent);
var catid = tagId.replace(/showpannel/, '');
getBrand(catid);
localStorage.setItem('brandcat', catid);
});
});
});