/
scrobMaster.js
236 lines (218 loc) · 7.03 KB
/
scrobMaster.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
/******* ScrobMaster *******/
/* ----
Author: Benjamin Miller (www.htmlben.com)
Version 0.0.2
*/
(function(){
function scrobMasterProto(){
this.triggerPos = 0;
this.state = {},
this.execute = function(funcs, key){
if(Object.prototype.toString.call(funcs) == "[object Array]"){
for(var i = 0, len = funcs.length; i<len; i++){
funcs[i].call(key, this.state);
}
return
}
funcs.call(key, this.state);
}
this.triggerPoint = function(){
return this.getScrollTop() + this.triggerPos;
}
this.getScrollTop = function (){
if(typeof pageYOffset != 'undefined')return pageYOffset;
else{
var B = document.body; //IE 'quirks'
var D = document.documentElement; //IE with doctype
D = (D.clientHeight)? D: B;
return D.scrollTop;
}
};
this.register = function(elmID){
var self = scrob;
if(elmID.substring(0,1)=="."){
var targetClass = elmID.substring(1);
self[targetClass] = new scrobarray();
self[targetClass].initiate(targetClass);
return self[targetClass];
}
if(elmID.substring(0,1)=="#"){
elmID = elmID.substring(1);
}
self[elmID] = new scrobject(elmID);
if(document.getElementById(elmID))self[elmID].initiate(elmID);
else{
try{window.addEventListener('load', function(){self[elmID].initiate(elmID);}, false)}
catch(err){window.attachEvent('onload', function(){self[elmID].initiate(elmID);})}
}
return self[elmID];
},
this.setTriggerPos = function(val){
scrob.triggerPos = val||0;
return this;
}
}
function scrobMaster(){}
scrobMaster.prototype = new scrobMasterProto();
function scrobjectProto(){
this.resolvedBufferTop = function(){return this.absoluteTop+this.bufferTop};
this.resolvedTop = function(){return this.absoluteTop+this.top};
this.resolvedBottom = function(){return this.absoluteBottom+this.bottom};
this.resolvedBufferBottom = function(){return this.absoluteBottom+this.bufferBottom};
this.getAbsoluteTop = function(){
var parent = this.elm.offsetParent,
top = this.elm.offsetTop;
while(parent !== document.body && parent !== document.getElementsByTagName('html')[0]){
top += parent.offsetTop;
parent = parent.offsetParent;
}
return top;
};
this.initiate = function(elmID){
var elm = (typeof elmID == 'string')? document.getElementById(elmID):elmID;
var ob = this;
ob.elm = elm;
ob.absoluteTop = (ob.absoluteTop)? ob.absoluteTop:ob.getAbsoluteTop();
ob.absoluteBottom = (ob.absoluteBottom)? ob.absoluteBottom:ob.getAbsoluteTop()+elm.offsetHeight;
ob.top = (ob.top)? ob.top:0;
ob.bottom = (ob.bottom)? ob.bottom:0;
ob.bufferTop = (ob.bufferTop)? ob.bufferTop:0;
ob.bufferBottom = (ob.bufferBottom)? ob.bufferBottom:0;
ob.style = elm.style;
}
this.addon = function(prop, val){
if(typeof prop == "string"){
if(this[prop]){
if(Object.prototype.toString.call(this[prop]) != "[object Array]"){
var holder = this[prop]
this[prop] = [];
this[prop].push(holder);
this[prop].push(val);
return this
}
this[prop].push(val);
return this;
}
return this.set(prop, val);
}
for(var key in prop){
if(prop.hasOwnProperty(key))this.addon(key, prop[key]);
}
return this
}
this.on = seter;
this.set = seter;
this.assign = function(prop, val){
this[prop] = val;
if(prop == "top" && (!this.bufferTop || val<this.bufferTop))this.bufferTop = val;
if(prop == "bottom" && (!this.bufferBottom || val>this.bufferBottom))this.bufferBottom = val;
}
function seter(prop, val){
if(typeof prop == "string"){
this.assign(prop, val);
return this;
}
for(var key in prop){
if(prop.hasOwnProperty(key))this.assign(key, prop[key]);
}
return this;
}
}
scrobjectProto.prototype = new scrobMasterProto();
function scrobject(elmID){}
scrobject.prototype = new scrobjectProto();
function scrobarrayProto(){
this.nodeInitiate = this.initiate;
this.initiate = function(classString){
var obar = this;
this.nodes = classString;
function ScrobArrayNode(){}
ScrobArrayNode.prototype = obar;
if(getClass(this.nodes) != undefined){gatherClasses()}
else{
if(window.addEventListener)window.addEventListener("load", gatherClasses, false);
else window.attachEvent("onload", gatherClasses);
}
function gatherClasses(){
var classHolder = getClass(obar.nodes);
obar.nodes = [];
for(var i = 0, len = classHolder.length; i<len; i++){
obar.nodes.push(new ScrobArrayNode());
obar.nodes[i].nodeInitiate(classHolder[i]);
}
}
};
function getClass(string){
if(document.getElementsByClassName){
var elms = document.getElementsByClassName(string);
return (elms.length == 0)? undefined:elms;
}
var elms = document.getElementsByTagName('*')
, match = new Array()
, regex = new RegExp("(^|\\s)"+string+"($|\\s)");
for(var i = 0, len = elms.length; i<len; i++){
if(elms[i].className.match(regex)){
match.push(elms[i]);
}
}
return (match.length > 0)? match:undefined;
}
}
scrobarrayProto.prototype = new scrobjectProto();
function scrobarray(){}
scrobarray.prototype = new scrobarrayProto();
window.scrob = new scrobMaster();
var updateScrollState = function(){
var ss = scrob.state;
ss.lastScrollTop = (ss.scrollTop)? ss.scrollTop:scrob.getScrollTop();
ss.scrollTop = scrob.getScrollTop();
ss.direction = (ss.scrollTop >= ss.lastScrollTop)? 'down':'up';
}
var scrollevent = function(){
var trigger = scrob.triggerPoint();
for(var key in scrob){
if(scrob.hasOwnProperty(key)){
if(scrob[key] instanceof scrobject){
scrobCheck(scrob[key], trigger);
}else if(scrob[key] instanceof scrobarray){
for(var i = 0, len = scrob[key].nodes.length; i<len; i++){
scrobCheck(scrob[key].nodes[i], trigger);
}
}
}
}
};
var scrobCheck = function(key, trigger){
if(key.resolvedBufferTop() < trigger && key.resolvedBufferBottom() > trigger){
if(!key.bufferEnetered){
if(key.bufferEnter)scrob.execute(key.bufferEnter, key);//Buffer Enter Event
key.bufferEnetered = true;
}
if(key.bufferStep)scrob.execute(key.bufferStep, key);//Buffer Step Event
if(key.resolvedTop() < trigger && key.resolvedBottom() > trigger){
if(!key.entered){
if(key.enter){scrob.execute(key.enter, key)}//key.enter();// Enter Event
key.entered = true;
}
if(key.step)scrob.execute(key.step, key);//Step Event
}else if(key.entered){
if(key.exit)scrob.execute(key.exit, key);//Exit Event
delete key.entered;
}//close live zone
}else if(key.bufferEnetered){
if(key.entered){if(key.exit)scrob.execute(key.exit, key);//Exit Event
delete key.entered;
}
if(key.bufferExit)scrob.execute(key.bufferExit, key);// Buffer Exit Event
delete key.bufferEnetered;
}//close buffer zone
}
if(window.addEventListener){
window.addEventListener('scroll', updateScrollState, false);
window.addEventListener('scroll', scrollevent, false);
}
else{
window.attachEvent('onscroll', scrollevent);
window.attachEvent('onscroll', updateScrollState);
}
})();