-
Notifications
You must be signed in to change notification settings - Fork 8
/
jquery.stickyHeaders.js
220 lines (167 loc) · 7.21 KB
/
jquery.stickyHeaders.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
;(function ($) {
// DEFAULTS
// -------------------------------------------------------------------------
var pluginName = 'stickyHeaders',
defaults = {
headlineSelector: '.sticky-header',
hiddenClass: 'sticky-header-hidden',
stickyElement: 'h2',
stickyClass: 'sticky-helper',
stickyChildren: '<span></span>',
textContainerSelector: 'span',
endOfScrollPos: null
};
// CONSTRUCTOR
// -------------------------------------------------------------------------
function StickyHeaders(el, options) {
this.$el = $(el);
this.options = $.extend({}, defaults, options);
// jQuery Objects
this.$headers = null;
// custom Objects consisting of jQuery Object, height, top offset and
// text
this.headers = [];
// header container count
this.headerLength = 0;
// sticky container
this.$sticky = null;
// sticky container text wrapper
this.$stickyText = null;
// position of wrapper for all headers
this.navWrapPos = 0;
// position where sticky-scrolling should end
this.endOfScrollPos = null;
// status container
this.lastStatus = {
isFixed: false,
index: 0
};
// let's get it on
this.init();
}
// PROTOTYPE
// -------------------------------------------------------------------------
StickyHeaders.prototype = {
// create sticky container and bind events
init: function() {
this.$sticky = $('<' + this.options.stickyElement + ' />')
.wrapInner(this.options.stickyChildren)
.addClass(this.options.stickyClass + ' ' + this.options.hiddenClass)
.prependTo(this.$el);
this.$stickyText = this.$sticky.find(this.options.textContainerSelector);
this.buildHeaderCache();
var _this = this;
$(window).on('load stickyHeadersRebuildCache', function() {
_this.buildHeaderCache();
}).on('scroll touchmove', function() {
_this.updateSticky();
});
},
// update sticky on scoll
updateSticky: function() {
var scrollPos = window.pageYOffset || window.scrollY,
i = 0;
// are we above first header?
if (scrollPos < this.headers[i].pos) {
if (this.lastStatus.index == i && !this.lastStatus.isFixed)
return;
this.disableSticky(this.headers[i].pos, i);
// are we below last header?
} else if (!!this.endOfScrollPos && scrollPos > this.endOfScrollPos - this.headers[this.headerLength - 1].height) {
if (this.lastStatus.index == this.headerLength - 1 && !this.lastStatus.isFixed)
return;
this.disableSticky(
this.endOfScrollPos - this.headers[this.headerLength - 1].height,
this.headerLength - 1
);
// we are between start of first and end of last header
} else {
var updateComplete = false;
for (i = this.headerLength - 1; !updateComplete; i--) {
// are we in transition between 2 headers?
if (i + 1 < this.headerLength && scrollPos + this.headers[i].height >= this.headers[i+1].pos) {
if (this.lastStatus.index == i && !this.lastStatus.isFixed)
return;
this.disableSticky(this.headers[i+1].pos - this.headers[i].height, i);
updateComplete = true;
// are we below current header?
} else if (scrollPos >= this.headers[i].pos) {
if (this.lastStatus.index == i && this.lastStatus.isFixed)
return;
this.enableSticky(i);
updateComplete = true;
}
}
}
},
// make sticky container fixed and position it at top
enableSticky: function(currentIndex) {
this.$sticky
.removeClass(this.options.hiddenClass)
.addClass('is-sticky')
.css({
position: 'fixed',
top: 0
});
this.updateTextAndClassesAndStatus(currentIndex, true);
},
// make sticky container absolute and position it accordingly
disableSticky: function(targetPosition, currentIndex) {
this.$sticky
.removeClass(this.options.hiddenClass + ' is-sticky')
.css({
position: 'absolute',
top: targetPosition - this.navWrapPos
});
this.updateTextAndClassesAndStatus(currentIndex, false);
},
// when positioning is done: update sticky container text, header
// classes and status
updateTextAndClassesAndStatus: function(currentIndex, isFixed) {
this.$stickyText.text(this.headers[currentIndex].text);
this.$headers
.not(this.headers[currentIndex].$el)
.removeClass(this.options.hiddenClass);
this.headers[currentIndex].$el.addClass(this.options.hiddenClass);
this.lastStatus = {
isFixed: isFixed,
index: currentIndex
};
},
// recalculate header element's positions etc.
buildHeaderCache: function() {
var _this = this;
this.navWrapPos = this.$el.offset().top;
this.$headers = $(this.options.headlineSelector)
.not("." + this.options.stickyClass);
this.headerLength = this.$headers.length;
this.headers = [];
this.$headers.each(function(i, el) {
var $el = $(el);
_this.headers.push({
$el: $el,
height: $el.outerHeight(),
pos: $el.offset().top,
text: $el.text()
});
});
// fix for wrong endOfScrollPos when images take too long to render
if (!this.endOfScrollPos && !!this.options.endOfScrollPos)
this.endOfScrollPos = typeof this.options.endOfScrollPos == 'function'
? this.options.endOfScrollPos()
: this.options.endOfScrollPos;
}
};
// REGISTER PLUGIN
// -------------------------------------------------------------------------
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(
this, 'plugin_' + pluginName,
new StickyHeaders(this, options)
);
}
});
};
})(jQuery);