-
Notifications
You must be signed in to change notification settings - Fork 25
/
waterfall.js
executable file
·431 lines (333 loc) · 12.1 KB
/
waterfall.js
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
420
421
422
423
424
425
426
427
428
429
430
431
(function() {
var root = (typeof self == 'object' && self.self == self && self) ||
(typeof global == 'object' && global.global == global && global) ||
this || {};
// 修复 bind 函数
Function.prototype.bind = Function.prototype.bind || function (context) {
if (typeof this !== "function") {
throw new Error("Function.prototype.bind - what is trying to be bound is not callable");
}
var self = this;
var args = Array.prototype.slice.call(arguments, 1);
var fNOP = function () {};
var fBound = function () {
var bindArgs = Array.prototype.slice.call(arguments);
self.apply(this instanceof fNOP ? this : context, args.concat(bindArgs));
}
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
}
var util = {
extend: function(target) {
for (var i = 1, len = arguments.length; i < len; i++) {
for (var prop in arguments[i]) {
if (arguments[i].hasOwnProperty(prop)) {
target[prop] = arguments[i][prop]
}
}
}
return target
},
isValidListener: function(listener) {
if (typeof listener === 'function') {
return true
} else if (listener && typeof listener === 'object') {
return isValidListener(listener.listener)
} else {
return false
}
},
indexOf: function(array, item) {
if (array.indexOf) {
return array.indexOf(item);
}
else {
var result = -1;
for (var i = 0, len = array.length; i < len; i++) {
if (array[i] === item) {
result = i;
break;
}
}
return result;
}
}
};
function EventEmitter() {
this.__events = {}
}
EventEmitter.prototype.on = function(eventName, listener) {
if (!eventName || !listener) return;
if (!util.isValidListener(listener)) {
throw new TypeError('listener must be a function');
}
var events = this.__events;
var listeners = events[eventName] = events[eventName] || [];
var listenerIsWrapped = typeof listener === 'object';
// 不重复添加事件
if (util.indexOf(listeners, listener) === -1) {
listeners.push(listenerIsWrapped ? listener : {
listener: listener,
once: false
});
}
return this;
};
EventEmitter.prototype.once = function(eventName, listener) {
return this.on(eventName, {
listener: listener,
once: true
})
};
EventEmitter.prototype.off = function(eventName, listener) {
var listeners = this.__events[eventName];
if (!listeners) return;
var index;
for (var i = 0, len = listeners.length; i < len; i++) {
if (listeners[i] && listeners[i].listener === listener) {
index = i;
break;
}
}
if (typeof index !== 'undefined') {
listeners.splice(index, 1, null)
}
return this;
};
EventEmitter.prototype.emit = function(eventName, args) {
var listeners = this.__events[eventName];
if (!listeners) return;
for (var i = 0; i < listeners.length; i++) {
var listener = listeners[i];
if (listener) {
listener.listener.apply(this, args || []);
if (listener.once) {
this.off(eventName, listener.listener)
}
}
}
return this;
};
// 惰性函数 addEvent
var addEvent = (function() {
if (window.addEventListener) {
return function(elem, type, fn) {
elem.addEventListener(type, fn, false);
}
} else if (window.attachEvent) {
return function(elem, type, fn) {
elem.attachEvent('on' + type, fn);
}
}
})();
function WaterFall(opts) {
EventEmitter.call(this);
this.opts = util.extend({}, this.constructor.defaultopts, opts);
this._container = typeof this.opts.container === 'string' ? document.querySelector(this.opts.container) : this.opts.container;
this._pins = typeof this.opts.pins === 'string' ? document.querySelectorAll(this.opts.pins) : this.opts.pins;
this._loader = typeof this.opts.loader === 'string' ? document.querySelector(this.opts.loader) : this.opts.loader;
this.init();
}
WaterFall.VERSION = '1.0.0';
WaterFall.defaultopts = {
gapHeight: 20,
gapWidth: 20,
pinWidth: 216,
threshold: 100
}
var proto = WaterFall.prototype = new EventEmitter();
proto.constructor = WaterFall;
proto.init = function() {
// 计算有多少列
this.getColumnNum();
// 设置 container 居中
this.setContainer();
// 如果已经有图片,设置为瀑布流
if (this._pins.length > 0) {
this.setPosition(this._pins)
}
var self = this;
// 设置瀑布流
setTimeout(function() {
self.setWaterFall();
}, 0)
// 绑定滚动事件
this.bindScrollEvent();
};
proto.getColumnNum = function() {
this._unitWidth = this.opts.pinWidth + this.opts.gapWidth;
this._viewPortWidth = window.innerWidth || document.documentElement.clientWidth;
this._viewPortHeight = window.innerHeight || document.documentElement.clientHeight;
this._num = Math.floor((this._viewPortWidth + this.opts.gapWidth) / this._unitWidth);
// 用于储存每列的高度,起始都为 0
this._columnHeightArr = [];
for (var i = 0; i < this._num; i++) {
this._columnHeightArr[i] = 0;;
}
};
/**
* 计算并且设置 container 宽度,使其居中
*/
proto.setContainer = function() {
this._container.style.width = (this._unitWidth * this._num - this.opts.gapWidth) + 'px';
};
/**
* 获取高度数组中的最小值,用于确定下个 pin 插入到那一列中
*/
proto.getMin = function() {
return Math.min.apply(null, this._columnHeightArr);
};
/**
* 获取高度数组中的最大值,用于设置 loading 的 top 值
*/
proto.getMax = function() {
return Math.max.apply(null, this._columnHeightArr);
};
// 保证一次只进行一次加载
var load = false;
proto.appendPins = function() {
if (load) return;
load = true;
// 显示 loading
if (this._loader) {
this._loader.style.display = 'block';
this._loader.style.top = (this.getMax() + 50) + 'px';
this._loader.style.left = '50%';
}
// 保证短时间内只触发一次
this.emit("load");
};
proto.append = function(html, selector) {
this._checkResult = [];
this._newPins = [];
var div = document.createElement("div")
div.innerHTML = html;
children = div.querySelectorAll(this.opts.pins)
var fragment = document.createDocumentFragment();
for (var j = 0, len = children.length; j < len; j++) {
fragment.appendChild(children[j])
this._checkResult[j] = false;
this._newPins.push(children[j])
this._checkImgHeight(children[j], selector, j)
}
this.isReadyAppend(fragment)
};
proto._checkImgHeight = function(childNode, selector, index) {
var startTime = new Date().getTime();
var img = childNode.querySelector(selector);
var self = this;
// 本地图片会先执行 onload 事件
img.onload = function() {
if (img.getAttribute('height')) return;
// 得到高度后,设置高度
img.setAttribute('height', Math.floor(img.height / img.width * self.opts.pinWidth));
// 通过标志量表示该图片已经设置了高度
self._checkResult[index] = true
clearInterval(timer)
}
img.onerror = function() {
if (img.getAttribute('height')) return;
img.setAttribute('height', 250);
self._checkResult[index] = true
clearInterval(timer)
}
if (img.getAttribute('height')) return img;
// 通过设置 interval 来最快得到加载中的图片高度
var check = function() {
if (img.width > 0 && img.height > 0) {
img.setAttribute('height', Math.floor(img.height / img.width * self.opts.pinWidth));
self._checkResult[index] = true
clearInterval(timer)
}
}
var timer = setInterval(check, 40)
};
proto.isReadyAppend = function(fragment) {
// 只有当所有图片都具有高度的时候,才添加进文档树
var self = this;
var checkAllHaveHeight = function() {
if (util.indexOf(self._checkResult, false) == -1) {
self._container.appendChild(fragment);
// 可以加载新的数据
load = false;
// 隐藏 loading
if (self._loader) {
self._loader.style.display = 'none';
}
// 对新添加的 pins 设置位置
self.setPosition(self._newPins);
clearTimeout(timer)
} else {
setTimeout(checkAllHaveHeight)
}
}
var timer = setTimeout(checkAllHaveHeight, 40);
};
/**
* 设置新的 pins 的位置
*/
proto.setPosition = function(pins) {
for (var i = 0, len = pins.length; i < len; i++) {
var min = this.getMin();
var index = util.indexOf(this._columnHeightArr, min);
pins[i].style.left = this._unitWidth * index + 'px';
pins[i].style.top = min + 'px';
this._columnHeightArr[index] += (pins[i].offsetHeight + this.opts.gapHeight);
}
this._newPins = [];
this.setWaterFall()
};
/**
* 判断是否需要添加新的 pins,在 init 的时候会调用一次,保证首屏充满
*/
proto.setWaterFall = function() {
if (this.getMin() < this._viewPortHeight) {
this.appendPins();
}
};
proto.bindScrollEvent = function() {
addEvent(window, "scroll", this.handleScroll.bind(this));
addEvent(window, "resize", this.handleResize.bind(this))
};
var timer = null;
proto.handleResize = function() {
var self = this;
clearTimeout(timer);
timer = setTimeout(function() {
self.resetPosition()
}, 100)
};
proto.resetPosition = function() {
// 计算有多少列
this.getColumnNum();
// 设置 container 居中
this.setContainer();
// 设置瀑布流
this.setPosition(typeof this.opts.pins === 'string' ? document.querySelectorAll(this.opts.pins) : this.opts.pins);
};
/**
* 只要有空白处,就可以加载新的数据
*/
proto.checkScroll = function() {
if (this.getMin() - (window.pageYOffset || document.documentElement.scrollTop) < this._viewPortHeight + this.opts.threshold) {
return true
}
return false;
};
proto.handleScroll = function() {
var self = this;
console.log(self.checkScroll())
if (self.checkScroll()) {
self.appendPins();
}
};
if (typeof exports != 'undefined' && !exports.nodeType) {
if (typeof module != 'undefined' && !module.nodeType && module.exports) {
exports = module.exports = WaterFall;
}
exports.WaterFall = WaterFall;
} else {
root.WaterFall = WaterFall;
}
}());