-
Notifications
You must be signed in to change notification settings - Fork 37
/
jquery.scrollSections.js
567 lines (490 loc) · 15.3 KB
/
jquery.scrollSections.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
;(function ($, window, Math, undefined) {
'use strict';
/**
* The name of this jQuery plugin.
*
* @type String
*/
var pluginName = 'scrollSections';
/**
* The default options of this plugin.
*
* @type Object
*/
var defaults = {
// Attribute from which we retrieve the unique identifier for each section.
attr: 'id',
// The class that should be applied to the current navigation item.
active: 'active-scrollsection',
// Enable keyboard controls.
keyboard: true,
// Enable mousehweel controls.
mousewheel: true,
// Enable touch controls.
touch: true,
// Enable scrollbar controls.
scrollbar: true,
// Enable navigation controls, also see createNavigation option.
navigation: true,
// Maximum sections to scroll within mousewheel interaction.
scrollMax: 1,
// Function to execute before each scroll.
before: null,
// Function to execute after each scroll.
after: null,
// Prefix for classes and ids of DOM elements.
prefix: 'scrollsections',
// Scroll to first section on initialization, instead of the section that is visible. Also have a look at the option
// animateScrollToFirstSection.
alwaysStartWithFirstSection: false,
// Scroll to initial section without animation.
animateScrollToFirstSection: false,
// Create navigation? If the option navigation is set to false, this will have no effect!
createNavigation: true,
// The animation speed.
speed: 500,
// Throw execption if something goes wrong.
exceptions: false
};
/**
* Catchable exception if something goes wrong.
*
* @param {String} message
* A message describing what went wrong.
* @returns {ScrollSectionsException}
*/
function ScrollSectionsException(message) {
this.name = 'ScrollSectionsException';
this.message = message;
}
/**
* Instantiate new instance of the scrollSections plugin.
*
* @param {jQuery} elements
* The DOM elements that make up our sections.
* @param {Object} options
* [Optional] Overwrite default options of the plugin.
* @returns {Plugin}
*/
var Plugin = function(elements, options) {
this.elements = elements;
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this._$window = $(window);
this._$htmlBody = $('html, body');
this._$body = $('body');
this._sections = elements.length;
this._$sections = [];
this._sectionIdentifiers = [];
this._delayFirstScroll = null;
this._$previousSection = null;
this._$currentSection = null;
this._currentStep = 0;
this._isFirstSection = true;
this._isAnimated = false;
this._wheelDelay = null;
this._scrollPaused = false;
this._$nav = null;
this._ltIE9 = false;
// Enough properties, start!
this.init();
};
Plugin.prototype = {
/**
* Create navigation.
*
* @returns {Plugin}
*/
createNavigation: function () {
// Only continue if we are supposed to listen on the navigation (otherwise creating it makes no sense).
if (this.options.navigation) {
// Create the navigation DOM element.
this._$nav = $('<nav>', { id: this.options.prefix + '-navigation' });
// Add links to each section to the navigation.
for (var i = 0; i < this._sections; i++) {
this._$nav.append($('<a>', {
id: this.options.prefix + '-menuitem-' + i,
'class': this.options.prefix + '-menuitem',
href: '#' + this._sectionIdentifiers[i],
html: 'Section ' + i
}));
}
// Append the newly created navigation to the body of the DOM.
this._$body.append(this._$nav);
}
return this;
},
/**
* Initialize event listeners for the navigation's menuitems.
*
* @returns {Plugin}
*/
navigation: function () {
var self = this;
// If we have no navigation, try to get one.
if (this._$nav === null) {
// The user wants us to create it for him, no problem.
if (this.options.createNavigation) {
this.createNavigation();
this.options.createNavigation = false;
// Add the sections index to each menuitem.
this._$nav._$menuitems = $('a', this._$nav).each(function (index) {
var $this = $(this);
// If this menuitem is the currently active one, add the class so our CSS knows about this as well.
if (index === self._currentStep) {
$this.addClass(self.options.active);
}
$this.data(self.options.prefix, index);
});
}
// Navigation is already present in the DOM, try to get it.
else {
this._$nav = $('#' + this.options.prefix + '-navigation');
this._$nav._$menuitems = $('a', this._$nav).each(function(){
var $this = $(this),
sectionIndex = self._sectionIdentifiers.indexOf($this.attr('href').substr(1));
if(sectionIndex===self._currentStep){
$this.addClass(self.options.active);
}
// assign section index to menu item
$this.data(self.options.prefix, sectionIndex);
});
}
}
// Well, do we have a navigation now?
if (this._$nav != null && this._$nav.length > 0) {
this._$nav._$menuitems.click(function (event) {
var $this = $(this),
sectionIndex = parseInt($this.data(self.options.prefix), 10);
event.preventDefault();
self._$nav._$menuitems.removeClass(self.options.active);
$this.addClass(self.options.active);
if(sectionIndex>=0){
self.customScrollTo(parseInt($this.data(self.options.prefix), 10));
}
else if(self.options.exceptions){
throw new ScrollSectionsException('Section not find for this menu item, make sure the href is the same as the section id');
}
return false;
});
}
return this;
},
/**
* Initialize scrollbar event listener.
*
* @returns {Plugin}
*/
scrollbar: function () {
var self = this;
// Check if the jQuery special events are present.
if (!$.event.special.scrollstop) {
if (this.options.exceptions) {
throw new ScrollSectionsException('The jQuery special events scrollstop plugin is missing.');
} else {
return this;
}
}
this._$window.bind('scrollstop', function (event) {
var scrollTop = self._$htmlBody.scrollTop() || self._$window.scrollTop();
var diff = self._$htmlBody.outerHeight();
var nextStep;
var diffTmp;
self._scrollPaused = false;
if (scrollTop === 0 && self._currentStep !== 0) {
nextStep = 0;
} else if ((scrollTop === (self._sections - 1) * self._$window.height()) && self._currentStep !== (self._sections - 1)) {
nextStep = self._sections - 1;
} else {
for (var i = 0; i < self._sections; i++) {
diffTmp = Math.abs(scrollTop - self._$sections[i].offset().top);
if (!diff || diffTmp <= diff) {
diff = diffTmp;
nextStep = i;
// Already scrolled!
if (diff === 0) {
return;
}
}
}
}
if (nextStep > -1) {
self.customScrollTo(nextStep);
}
});
return this;
},
/**
* Initialize touch event listener.
*
* @returns {Plugin}
*/
touch: function () {
var self = this;
this._$body.bind('touchstart', function (event) {
var startEvent = event;
event.preventDefault();
self._$body.bind('touchmove', function (event) {
var diff = { x: startEvent.clientX - event.clientX, y: startEvent.clientY - event.clientY };
var nextStep;
event.preventDefault();
if ((diff.y <= -100 || diff.y >= 100) && Math.abs(diff.y) > Math.abs(diff.x)) {
nextStep = diff.y < 0 ? self._currentStep - 1 : self._currentStep + 1;
self.customScrollTo(nextStep);
}
return false;
});
return false;
});
return this;
},
/**
* Initialize keyboard event listener.
*
* @returns {Plugin}
*/
keyboard: function () {
var self = this;
this._$htmlBody.keydown(function (event) {
var nextStep;
switch (event.which) {
case 33: // page up
case 36: // pos 1
event.preventDefault();
self.customScrollTo(0);
return false;
case 34: // page down
case 35: // end
event.preventDefault();
self.customScrollTo(self._sections - 1);
return false;
case 38: // up
event.preventDefault();
nextStep = self._currentStep - 1;
if (nextStep >= 0) {
self.customScrollTo(nextStep);
}
return false;
case 40: // down
event.preventDefault();
nextStep = self._currentStep + 1;
if (nextStep < self._sections) {
self.customScrollTo(nextStep);
}
return false;
}
});
return this;
},
/**
* Call any callbacks that the user defined (before or after)
*
* @param {Boolean} before
* Set to true if the before callback should be called.
* @returns {Plugin}
*/
scrollCallback: function (before) {
this._isAnimated = before || false;
if (before && this.options.before) {
this.options.before(this._$previousSection, this._$currentSection);
} else if (!before && this.options.after) {
this.options.after(this._$currentSection, this._$previousSection);
}
return this;
},
/**
* Scroll to the section identified by given index.
*
* @param {Number} index
* The index of the section to which we should scroll.
* @param {Boolean} noAnimation
* Set to true if there should be no animation at all.
* @returns {Plugin}
*/
customScrollTo: function (index, noAnimation) {
var self = this;
var yTo;
var speed;
if (index != null && index >= 0 && index < this._sections) {
this._currentStep = index;
this._$previousSection = this._$currentSection;
this._$currentSection = this._$sections[index];
yTo = this._$currentSection.offset().top;
speed = noAnimation ? 0 : this.options.speed;
// Mark any link on the page that refers to our active section active.
if (this._$nav) {
this._$nav._$menuitems.removeClass(this.options.active);
$('a[href="#' + this._sectionIdentifiers[index] + '"]', this._$nav).addClass(this.options.active);
}
// Call the before callback, stop any ongoing animation and animate to our current new section.
this.scrollCallback(true);
// Using deferred object otherwise callback happens twice because we are animating 2 jQuery objects for cross-browser compatibiity
$.when(
this._$htmlBody.stop(true, false).animate({ scrollTop: yTo }, speed)
).done(function(){
// Call the after callback
self.scrollCallback();
});
}
return this;
},
/**
* Helper method for the mousewheel.
*
* @param {Number} index
* The index of the section to which we should scroll.
* @returns {Plugin}
*/
mousewheelScrollTo: function (index) {
this.customScrollTo(index);
this._wheelDelay = null;
this._scrollPaused = true;
return this;
},
/**
* Initialize mousewheel event listener.
*
* @returns {Plugin}
*/
mousewheel: function () {
var self = this;
// No support for broken clients!
if (this._ltIE9) {
// Throwing an exception allows the user to catch and do something with it (e.g. display an alert message to the
// user).
if (this.options.exceptions) {
throw new ScrollSectionsException('Cannot bind mousewheel on broken client.');
}
// Silently do nothing.
else {
return this;
}
}
// Check if the jQuery mousewheel plugin is present.
if (!$.fn.mousewheel) {
if (this.options.exceptions) {
throw new ScrollSectionsException('The jQuery mousewheel plugin is missing.');
} else {
return this;
}
}
this._$window.mousewheel(function (event, delta, deltaX, deltaY) {
var stepDiff = null;
var nextStep = -1;
event.preventDefault();
// Only scroll if we are not animating and scrolling is not paused.
if (!(self._isAnimated && self._scrollPaused)) {
deltaY = deltaY>>0; // Because steps numbers are integers
// Scroll Down
if (deltaY < 0) {
// Only allow the user to scroll down the maximum sections as defined in our options.
if (deltaY < -self.options.scrollMax) {
deltaY = -self.options.scrollMax;
}
if ((!nextStep || !stepDiff || deltaY < stepDiff) && ((self._currentStep - deltaY) < self._sections)) {
stepDiff = deltaY;
nextStep = self._currentStep - stepDiff;
}
}
// Scroll Up
else {
// Only allow the user to scroll up the maximum sections as defined in our options.
if (deltaY > self.options.scrollMax) {
deltaY = self.options.scrollMax;
}
if ((!nextStep || !stepDiff || deltaY > stepDiff) && ((self._currentStep - deltaY) > -1)) {
stepDiff = deltaY;
nextStep = self._currentStep - stepDiff;
}
}
if (stepDiff && nextStep > -1) {
if (self._wheelDelay) {
clearTimeout(self._wheelDelay);
}
if (Math.abs(stepDiff) < self.options.scrollMax) {
self._wheelDelay = setTimeout(function () {
self.mousewheelScrollTo(nextStep);
}, 10);
} else {
self.mousewheelScrollTo(nextStep);
}
}
}
return false;
});
return this;
},
/**
* Initialize our plugin.
*
* @returns {Plugin}
*/
init: function () {
/**
* Keep reference to our plugin instance.
*
* @type {Plugin}
*/
var self = this;
// Only continue if we have any sections to compute.
if (this._sections > 0) {
// Loop over all DOM elements with which our plugin was initialized and fill our arrays.
this.elements.each(function (index) {
// Cache jQuery Object creation.
var $section = $(this);
var windowScrollTop = self._$window.scrollTop();
var offset;
self._$sections[index] = $section;
self._sectionIdentifiers[index] = $section.attr(self.options.attr);
if (self.options.alwaysStartWithFirstSection === true && index === 0) {
$section.addClass(self.options.active);
self._$currentSection = $section;
self.customScrollTo(0, !self.options.animateScrollToFirstSection);
} else {
offset = $section.offset();
offset.bottom = offset.top + $section.height();
// Check which section is active and change the current section plus trigger active state.
if (windowScrollTop >= offset.top && windowScrollTop < offset.bottom) {
$section.addClass(self.options.active);
self._currentStep = index;
self._$currentSection = $section;
if (windowScrollTop !== offset.top) {
self.customScrollTo(index);
}
}
}
});
// Check if we are working if a broken client.
if ((new RegExp('MSIE ([0-9]{1,}[\\.0-9]{0,})')).exec(window.navigator.userAgent) && (parseFloat(RegExp.$1) <= 8.0)) {
this._ltIE9 = true;
}
// Execute each control initializer that was set to true.
for (var o in this.options) {
if (this.options[o] === true && typeof this[o] === 'function') {
this[o]();
}
}
// Bind to resize event and keep the current section always within the viewport.
this._$window.resize(function () {
self.customScrollTo(self._currentStep);
});
}
return this;
}
};
/**
* Lightweight wrapper around our constructor, prevent multiple instantiation of our plugin.
*
* @param {Object} options
* [Optional] Overwrite the default options of the scrollSections plugin.
* @returns {jQuery}
*/
$.fn[pluginName] = function (options) {
// Only create a new instance if none has been registered so far.
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin(this, options));
}
return this;
};
})(jQuery, window, Math);