forked from liulangnan/aui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaui-scroll.js
66 lines (65 loc) · 1.83 KB
/
aui-scroll.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
/**
* aui-scroll.js
* @author 流浪男
* @todo more things to abstract, e.g. Loading css etc.
* Licensed under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*/
(function(window) {
'use strict';
var isToBottom = false,isMoved = false;
var auiScroll = function (params,callback) {
this.extend(this.params, params);
this._init(params,callback);
}
auiScroll.prototype = {
params: {
listren:false,
distance: 100
},
_init : function(params,callback) {
var self = this;
if(self.params.listen){
document.body.addEventListener("touchmove", function(e){
self.scroll(callback);
});
document.body.addEventListener("touchend", function(e){
self.scroll(callback);
});
}
window.onscroll = function(){
self.scroll(callback);
}
},
scroll : function (callback) {
var self = this;
var clientHeight = document.documentElement.scrollTop === 0 ? document.body.clientHeight : document.documentElement.clientHeight;
var scrollTop = document.documentElement.scrollTop === 0 ? document.body.scrollTop : document.documentElement.scrollTop;
var scrollHeight = document.documentElement.scrollTop === 0 ? document.body.scrollHeight : document.documentElement.scrollHeight;
if (scrollHeight-scrollTop-self.params.distance <= window.innerHeight) {
isToBottom = true;
if(isToBottom){
callback({
"scrollTop":scrollTop,
"isToBottom":true
})
}
}else{
isToBottom = false;
callback({
"scrollTop":scrollTop,
"isToBottom":false
})
}
},
extend: function(a, b) {
for (var key in b) {
if (b.hasOwnProperty(key)) {
a[key] = b[key];
}
}
return a;
}
}
window.auiScroll = auiScroll;
})(window);