-
Notifications
You must be signed in to change notification settings - Fork 6
/
sticky.js
executable file
·244 lines (210 loc) · 7.43 KB
/
sticky.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
(function() {
var root = (typeof self == 'object' && self.self == self && self) ||
(typeof global == 'object' && global.global == global && global) ||
this || {};
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
},
getStyle: function(element, prop) {
return element.currentStyle ? element.currentStyle[prop] : document.defaultView.getComputedStyle(element)[prop]
},
getScrollOffsets: function() {
var w = window;
if (w.pageXOffset != null) return { x: w.pageXOffset, y: w.pageYOffset };
var d = w.document;
if (document.compatMode == "CSS1Compat") {
return {
x: d.documentElement.scrollLeft,
y: d.documentElement.scrollTop
}
}
return { x: d.body.scrollLeft, y: d.body.scrollTop }
},
addEvent: function(element, type, fn) {
if (document.addEventListener) {
element.addEventListener(type, fn, false);
return fn;
} else if (document.attachEvent) {
var bound = function() {
return fn.apply(element, arguments)
}
element.attachEvent('on' + type, bound);
return bound;
}
},
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;
}
},
addClass: function(element, className) {
var classNames = element.className.split(/\s+/);
if (util.indexOf(classNames, className) == -1) {
classNames.push(className);
}
element.className = classNames.join(' ')
},
removeClass: function(element, className) {
var classNames = element.className.split(/\s+/);
var index = util.indexOf(classNames, className)
if (index !== -1) {
classNames.splice(index, 1);
}
element.className = classNames.join(' ')
},
isValidListener: function(listener) {
if (typeof listener === 'function') {
return true
} else if (listener && typeof listener === 'object') {
return util.isValidListener(listener.listener)
} else {
return false
}
},
removeProperty: function(element, name) {
if (element.style.removeProperty) {
element.style.removeProperty(name);
} else {
element.style.removeAttribute(name);
}
}
};
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;
};
function Sticky(element, options) {
EventEmitter.call(this);
this.element = typeof element === "string" ? document.querySelector(element) : element;
this.options = util.extend({}, this.constructor.defaultOptions, options)
this.init();
}
Sticky.version = '1.0.0';
Sticky.defaultOptions = {
offset: 0
}
var proto = Sticky.prototype = new EventEmitter();
proto.constructor = Sticky;
proto.init = function() {
this.calculateElement();
this.bindScrollEvent();
};
proto.calculateElement = function() {
// 计算出元素距离文档的位置
if (this.element) {
var rect = this.element.getBoundingClientRect();
this.eLeft = rect.left + util.getScrollOffsets().x;
this.eTop = rect.top + util.getScrollOffsets().y - this.options.offset;
}
};
proto.bindScrollEvent = function() {
var self = this;
util.addEvent(window, "scroll", function(event) {
if (util.getScrollOffsets().y > self.eTop) {
self.setSticky();
} else {
self.setNormal();
}
})
};
proto.setSticky = function() {
if (this.status == "sticky") return;
this.status = "sticky";
util.addClass(this.element, 'sticky');
this.setElementSticky();
this.emit("onStick");
};
proto.setNormal = function() {
if (this.status !== "sticky") return;
this.status = "normal";
util.removeClass(this.element, 'sticky');
this.setElementNormal();
this.emit("onDetach");
};
proto.setElementSticky = function() {
this.element.style.position = "fixed";
this.element.style.left = this.eLeft + 'px';
this.element.style.top = this.options.offset + 'px';
};
proto.setElementNormal = function() {
util.removeProperty(this.element, "position")
util.removeProperty(this.element, "left")
util.removeProperty(this.element, "top")
};
if (typeof exports != 'undefined' && !exports.nodeType) {
if (typeof module != 'undefined' && !module.nodeType && module.exports) {
exports = module.exports = Sticky;
}
exports.Sticky = Sticky;
} else {
root.Sticky = Sticky;
}
}());