/
headhesive.js
150 lines (150 loc) · 4.83 KB
/
headhesive.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
/*!
* Headhesive.js v1.2.3 - An on-demand sticky header
* Author: Copyright (c) Mark Goodyear <@markgdyr> <http://markgoodyear.com>
* Url: http://markgoodyear.com/labs/headhesive
* License: MIT
*/
(function(root, factory) {
if (typeof define === "function" && define.amd) {
define([], function() {
return factory();
});
} else if (typeof exports === "object") {
module.exports = factory();
} else {
root.Headhesive = factory();
}
})(this, function() {
"use strict";
var _mergeObj = function(to, from) {
for (var p in from) {
if (from.hasOwnProperty(p)) {
to[p] = typeof from[p] === "object" ? _mergeObj(to[p], from[p]) : from[p];
}
}
return to;
};
var _throttle = function(func, wait) {
var _now = Date.now || function() {
return new Date().getTime();
};
var context, args, result;
var timeout = null;
var previous = 0;
var later = function() {
previous = _now();
timeout = null;
result = func.apply(context, args);
context = args = null;
};
return function() {
var now = _now();
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0) {
clearTimeout(timeout);
timeout = null;
previous = now;
result = func.apply(context, args);
context = args = null;
} else if (!timeout) {
timeout = setTimeout(later, remaining);
}
return result;
};
};
var _getScrollY = function() {
return window.pageYOffset !== undefined ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
};
var _getElemY = function(elem, side) {
var pos = 0;
var elemHeight = elem.offsetHeight;
while (elem) {
pos += elem.offsetTop;
elem = elem.offsetParent;
}
if (side === "bottom") {
pos = pos + elemHeight;
}
return pos;
};
var Headhesive = function(elem, options) {
if (!("querySelector" in document && "addEventListener" in window)) {
return;
}
this.visible = false;
this.options = {
offset: 300,
offsetSide: "top",
classes: {
clone: "headhesive",
stick: "headhesive--stick",
unstick: "headhesive--unstick"
},
throttle: 250,
onInit: function() {},
onStick: function() {},
onUnstick: function() {},
onDestroy: function() {}
};
this.elem = typeof elem === "string" ? document.querySelector(elem) : elem;
this.options = _mergeObj(this.options, options);
this.init();
};
Headhesive.prototype = {
constructor: Headhesive,
init: function() {
this.clonedElem = this.elem.cloneNode(true);
this.clonedElem.className += " " + this.options.classes.clone;
document.body.insertBefore(this.clonedElem, document.body.firstChild);
if (typeof this.options.offset === "number") {
this.scrollOffset = this.options.offset;
} else if (typeof this.options.offset === "string") {
this._setScrollOffset();
} else {
throw new Error("Invalid offset: " + this.options.offset);
}
this._throttleUpdate = _throttle(this.update.bind(this), this.options.throttle);
this._throttleScrollOffset = _throttle(this._setScrollOffset.bind(this), this.options.throttle);
window.addEventListener("scroll", this._throttleUpdate, false);
window.addEventListener("resize", this._throttleScrollOffset, false);
this.options.onInit.call(this);
},
_setScrollOffset: function() {
if (typeof this.options.offset === "string") {
this.scrollOffset = _getElemY(document.querySelector(this.options.offset), this.options.offsetSide);
}
},
destroy: function() {
document.body.removeChild(this.clonedElem);
window.removeEventListener("scroll", this._throttleUpdate);
window.removeEventListener("resize", this._throttleScrollOffset);
this.options.onDestroy.call(this);
},
stick: function() {
if (!this.visible) {
this.clonedElem.className = this.clonedElem.className.replace(new RegExp("(^|\\s)*" + this.options.classes.unstick + "(\\s|$)*", "g"), "");
this.clonedElem.className += " " + this.options.classes.stick;
this.visible = true;
this.options.onStick.call(this);
}
},
unstick: function() {
if (this.visible) {
this.clonedElem.className = this.clonedElem.className.replace(new RegExp("(^|\\s)*" + this.options.classes.stick + "(\\s|$)*", "g"), "");
this.clonedElem.className += " " + this.options.classes.unstick;
this.visible = false;
this.options.onUnstick.call(this);
}
},
update: function() {
if (_getScrollY() > this.scrollOffset) {
this.stick();
} else {
this.unstick();
}
}
};
return Headhesive;
});