-
Notifications
You must be signed in to change notification settings - Fork 40
/
scrollspy.js
242 lines (177 loc) · 7.87 KB
/
scrollspy.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
/*!
* Scrollspy Plugin
* Author: r3plica
* Licensed under the MIT license
*/
; (function ($, window, document, undefined) {
// Add our plugin to fn
$.fn.extend({
// Scrollspy is the name of the plugin
scrollspy: function (options) {
// Define our defaults
var defaults = {
namespace: 'scrollspy',
activeClass: 'active',
animate: false,
duration: 1000,
offset: 0,
container: window,
replaceState: false
};
// Add any overriden options to a new object
options = $.extend({}, defaults, options);
// Adds two numbers together
var add = function (ex1, ex2) {
return parseInt(ex1, 10) + parseInt(ex2, 10);
}
// Find our elements
var findElements = function (links) {
// Declare our array
var elements = [];
// Loop through the links
for (var i = 0; i < links.length; i++) {
// Get our current link
var link = links[i];
// Get our hash
var hash = $(link).attr("href");
// Store our has as an element
var element = $(hash);
// If we have an element matching the hash
if (element.length > 0) {
// Get our offset
var top = Math.floor(element.offset().top),
bottom = top + Math.floor(element.outerHeight());
// Add to our array
elements.push({ element: element, hash: hash, top: top, bottom: bottom });
}
}
// Return our elements
return elements;
};
// Find our link from a hash
var findLink = function (links, hash) {
// For each link
for (var i = 0; i < links.length; i++) {
// Get our current link
var link = $(links[i]);
// If our hash matches the link href
if (link.attr("href") === hash) {
// Return the link
return link;
}
}
};
// Reset classes on our elements
var resetClasses = function (links) {
// For each link
for (var i = 0; i < links.length; i++) {
// Get our current link
var link = $(links[i]);
// Remove the active class
link.parent().removeClass(options.activeClass);
}
};
// Store last fired scroll event
var scrollArea = '';
// For each scrollspy instance
return this.each(function () {
// Declare our global variables
var element = this,
container = $(options.container);
// Get our objects
var links = $(element).find('a');
// Loop through our links
for (var i = 0; i < links.length; i++) {
// Get our current link
var link = links[i];
// Bind the click event
$(link).on("click", function (e) {
// Get our target
var target = $(this).attr("href"),
$target = $(target);
// If we have the element
if ($target.length > 0) {
// Get it's scroll position
var top = add($target.offset().top, options.offset);
// If animation is on
if (options.animate) {
// Animate our scroll
$('html, body').animate({ scrollTop: top }, options.duration);
} else {
// Scroll to our position
window.scrollTo(0, top);
}
// Prevent our link
e.preventDefault();
}
});
}
// Set links
resetClasses(links);
// Get our elements
var elements = findElements(links);
var trackChanged = function() {
// Get the position and store in an object
var position = {
top: add($(this).scrollTop(), Math.abs(options.offset)),
left: $(this).scrollLeft()
};
// Create a variable for our link
var link;
// Loop through our elements
for (var i = 0; i < elements.length; i++) {
// Get our current item
var current = elements[i];
// If we are within the boundaries of our element
if (position.top >= current.top && position.top < current.bottom) {
// get our element
var hash = current.hash;
// Get the link
link = findLink(links, hash);
// If we have a link
if (link) {
// If we have an onChange function
if (options.onChange && (scrollArea !== hash)) {
// Fire our onChange function
options.onChange(current.element, $(element), position);
// set scrollArea
scrollArea = hash;
}
// Update url
if (options.replaceState) {
history.replaceState( {}, '', '/' + hash )
}
// Reset the classes on all other link
resetClasses(links);
// Add our active link to our parent
link.parent().addClass(options.activeClass);
// break our loop
break;
}
}
}
// If we don't have a link and we have a exit function
if (!link && (scrollArea !== 'exit') && options.onExit) {
// Fire our onChange function
options.onExit($(element), position);
// Reset the classes on all other link
resetClasses(links);
// set scrollArea
scrollArea = 'exit';
// Update url
if (options.replaceState) {
history.replaceState( {}, '', '/' )
}
}
}
// Add a listener to the window
container.bind('scroll.' + options.namespace, function () {
trackChanged();
});
$( document ).ready(function (e) {
trackChanged();
})
});
}
});
})(jQuery, window, document, undefined);