This repository has been archived by the owner on Apr 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathjquery-scrollspy.js
97 lines (78 loc) · 3.26 KB
/
jquery-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
/*!
* jQuery Scrollspy Plugin
* Author: @sxalexander
* Licensed under the MIT license
*/
;(function ( $, window, document, undefined ) {
$.fn.extend({
scrollspy: function ( options ) {
var defaults = {
min: 0,
max: 0,
mode: 'vertical',
namespace: 'scrollspy',
buffer: 0,
container: window,
onEnter: options.onEnter ? options.onEnter : [],
onLeave: options.onLeave ? options.onLeave : [],
onTick: options.onTick ? options.onTick : []
}
var options = $.extend( {}, defaults, options );
return this.each(function (i) {
var element = this;
var o = options;
var $container = $(o.container);
var mode = o.mode;
var buffer = o.buffer;
var enters = leaves = 0;
var inside = false;
/* add listener to container */
$container.bind('scroll.' + o.namespace, function(e){
var position = {top: $(this).scrollTop(), left: $(this).scrollLeft()};
var xy = (mode == 'vertical') ? position.top + buffer : position.left + buffer;
var max = o.max;
var min = o.min;
/* fix max */
if($.isFunction(o.max)){
max = o.max();
}
/* fix max */
if($.isFunction(o.min)){
min = o.min();
}
if(max == 0){
max = (mode == 'vertical') ? $container.height() : $container.outerWidth() + $(element).outerWidth();
}
/* if we have reached the minimum bound but are below the max ... */
if(xy >= min && xy <= max){
/* trigger enter event */
if(!inside){
inside = true;
enters++;
/* fire enter event */
$(element).trigger('scrollEnter', {position: position})
if($.isFunction(o.onEnter)){
o.onEnter(element, position);
}
}
/* trigger tick event */
$(element).trigger('scrollTick', {position: position, inside: inside, enters: enters, leaves: leaves})
if($.isFunction(o.onTick)){
o.onTick(element, position, inside, enters, leaves);
}
}else{
if(inside){
inside = false;
leaves++;
/* trigger leave event */
$(element).trigger('scrollLeave', {position: position, leaves:leaves})
if($.isFunction(o.onLeave)){
o.onLeave(element, position);
}
}
}
});
});
}
})
})( jQuery, window, document, undefined );