-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroller.js
65 lines (60 loc) · 1.67 KB
/
scroller.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
/**
* scroller
* 滚动事件监听器,优化句柄执行
*/
var $ = require('zepto');
var runtime = require('runtime');
var timer, time = new Date(), now;
$(window).scroll(function () {
if (timer) {
clearTimeout(timer);
}
now = new Date();
if(now - time > 500){
Scroller.onScroll();
time = now;
}else{
timer = setTimeout(function () {
Scroller.onScroll();
}, 100);
}
});
runtime.vars.scrollObject = {
scrollTop: document.documentElement.scrollTop + document.body.scrollTop,
documentHeight: $(document).height(),
windowHeight: $(window).height(),
scrollHeight: document.body.scrollHeight
};
var Scroller = {
handlers: {},
bind: function (pageId, handler) {
if (!this.handlers[pageId]) {
this.handlers[pageId] = {};
}
this.handlers[pageId][handler] = handler;
},
unbind: function (pageId, handler) {
delete this.handlers[pageId][handler];
},
unload: function (pageId) {
this.handlers[pageId] = null;
},
onScroll: function () {
var evtObj = {
scrollTop: document.documentElement.scrollTop + document.body.scrollTop,
documentHeight: $(document).height(),
windowHeight: $(window).height(),
scrollHeight: document.body.scrollHeight
},
pageId = runtime.env.page.id,
handlers = this.handlers[pageId];
runtime.vars.scrollObject = evtObj;
if (!handlers) {
return;
}
$.each(handlers, function (_, handler) {
handler(evtObj);
});
}
}
module.exports = Scroller;