Blame view

bower_components/Materialize/js/scrollspy.js 7.76 KB
74249687   Luigi Serra   Cross browser con...
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
  /**
   * Extend jquery with a scrollspy plugin.
   * This watches the window scroll and fires events when elements are scrolled into viewport.
   *
   * throttle() and getTime() taken from Underscore.js
   * https://github.com/jashkenas/underscore
   *
   * @author Copyright 2013 John Smart
   * @license https://raw.github.com/thesmart/jquery-scrollspy/master/LICENSE
   * @see https://github.com/thesmart
   * @version 0.1.2
   */
  (function($) {
  
  	var jWindow = $(window);
  	var elements = [];
  	var elementsInView = [];
  	var isSpying = false;
  	var ticks = 0;
  	var unique_id = 1;
  	var offset = {
  		top : 0,
  		right : 0,
  		bottom : 0,
  		left : 0,
  	}
  
  	/**
  	 * Find elements that are within the boundary
  	 * @param {number} top
  	 * @param {number} right
  	 * @param {number} bottom
  	 * @param {number} left
  	 * @return {jQuery}		A collection of elements
  	 */
  	function findElements(top, right, bottom, left) {
  		var hits = $();
  		$.each(elements, function(i, element) {
  			if (element.height() > 0) {
  				var elTop = element.offset().top,
  					elLeft = element.offset().left,
  					elRight = elLeft + element.width(),
  					elBottom = elTop + element.height();
  
  				var isIntersect = !(elLeft > right ||
  					elRight < left ||
  					elTop > bottom ||
  					elBottom < top);
  
  				if (isIntersect) {
  					hits.push(element);
  				}
  			}
  		});
  
  		return hits;
  	}
  
  
  	/**
  	 * Called when the user scrolls the window
  	 */
  	function onScroll() {
  		// unique tick id
  		++ticks;
  
  		// viewport rectangle
  		var top = jWindow.scrollTop(),
  			left = jWindow.scrollLeft(),
  			right = left + jWindow.width(),
  			bottom = top + jWindow.height();
  
  		// determine which elements are in view
  //        + 60 accounts for fixed nav
  		var intersections = findElements(top+offset.top + 200, right+offset.right, bottom+offset.bottom, left+offset.left);
  		$.each(intersections, function(i, element) {
  
  			var lastTick = element.data('scrollSpy:ticks');
  			if (typeof lastTick != 'number') {
  				// entered into view
  				element.triggerHandler('scrollSpy:enter');
  			}
  
  			// update tick id
  			element.data('scrollSpy:ticks', ticks);
  		});
  
  		// determine which elements are no longer in view
  		$.each(elementsInView, function(i, element) {
  			var lastTick = element.data('scrollSpy:ticks');
  			if (typeof lastTick == 'number' && lastTick !== ticks) {
  				// exited from view
  				element.triggerHandler('scrollSpy:exit');
  				element.data('scrollSpy:ticks', null);
  			}
  		});
  
  		// remember elements in view for next tick
  		elementsInView = intersections;
  	}
  
  	/**
  	 * Called when window is resized
  	*/
  	function onWinSize() {
  		jWindow.trigger('scrollSpy:winSize');
  	}
  
  	/**
  	 * Get time in ms
     * @license https://raw.github.com/jashkenas/underscore/master/LICENSE
  	 * @type {function}
  	 * @return {number}
  	 */
  	var getTime = (Date.now || function () {
  		return new Date().getTime();
  	});
  
  	/**
  	 * Returns a function, that, when invoked, will only be triggered at most once
  	 * during a given window of time. Normally, the throttled function will run
  	 * as much as it can, without ever going more than once per `wait` duration;
  	 * but if you'd like to disable the execution on the leading edge, pass
  	 * `{leading: false}`. To disable execution on the trailing edge, ditto.
  	 * @license https://raw.github.com/jashkenas/underscore/master/LICENSE
  	 * @param {function} func
  	 * @param {number} wait
  	 * @param {Object=} options
  	 * @returns {Function}
  	 */
  	function throttle(func, wait, options) {
  		var context, args, result;
  		var timeout = null;
  		var previous = 0;
  		options || (options = {});
  		var later = function () {
  			previous = options.leading === false ? 0 : getTime();
  			timeout = null;
  			result = func.apply(context, args);
  			context = args = null;
  		};
  		return function () {
  			var now = getTime();
  			if (!previous && options.leading === false) previous = 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 && options.trailing !== false) {
  				timeout = setTimeout(later, remaining);
  			}
  			return result;
  		};
  	};
  
  	/**
  	 * Enables ScrollSpy using a selector
  	 * @param {jQuery|string} selector  The elements collection, or a selector
  	 * @param {Object=} options	Optional.
          throttle : number -> scrollspy throttling. Default: 100 ms
          offsetTop : number -> offset from top. Default: 0
          offsetRight : number -> offset from right. Default: 0
          offsetBottom : number -> offset from bottom. Default: 0
          offsetLeft : number -> offset from left. Default: 0
  	 * @returns {jQuery}
  	 */
  	$.scrollSpy = function(selector, options) {
  		var visible = [];
  		selector = $(selector);
  		selector.each(function(i, element) {
  			elements.push($(element));
  			$(element).data("scrollSpy:id", i);
  			// Smooth scroll to section
  		  $('a[href=#' + $(element).attr('id') + ']').click(function(e) {
  		    e.preventDefault();
  		    var offset = $(this.hash).offset().top + 1;
  
  //          offset - 200 allows elements near bottom of page to scroll
  			
  	    	$('html, body').animate({ scrollTop: offset - 200 }, {duration: 400, queue: false, easing: 'easeOutCubic'});
  			
  		  });
  		});
  		options = options || {
  			throttle: 100
  		};
  
  		offset.top = options.offsetTop || 0;
  		offset.right = options.offsetRight || 0;
  		offset.bottom = options.offsetBottom || 0;
  		offset.left = options.offsetLeft || 0;
  
  		var throttledScroll = throttle(onScroll, options.throttle || 100);
  		var readyScroll = function(){
  			$(document).ready(throttledScroll);
  		};
  
  		if (!isSpying) {
  			jWindow.on('scroll', readyScroll);
  			jWindow.on('resize', readyScroll);
  			isSpying = true;
  		}
  
  		// perform a scan once, after current execution context, and after dom is ready
  		setTimeout(readyScroll, 0);
  
  
  		selector.on('scrollSpy:enter', function() {
  			visible = $.grep(visible, function(value) {
  	      return value.height() != 0;
  	    });
  
  			var $this = $(this);
  
  			if (visible[0]) {
  				$('a[href=#' + visible[0].attr('id') + ']').removeClass('active');
  				if ($this.data('scrollSpy:id') < visible[0].data('scrollSpy:id')) {
  					visible.unshift($(this));
  				}
  				else {
  					visible.push($(this));
  				}
  			}
  			else {
  				visible.push($(this));
  			}
  
  
  			$('a[href=#' + visible[0].attr('id') + ']').addClass('active');
  		});
  		selector.on('scrollSpy:exit', function() {
  			visible = $.grep(visible, function(value) {
  	      return value.height() != 0;
  	    });
  
  			if (visible[0]) {
  				$('a[href=#' + visible[0].attr('id') + ']').removeClass('active');
  				var $this = $(this);
  				visible = $.grep(visible, function(value) {
  	        return value.attr('id') != $this.attr('id');
  	      });
  	      if (visible[0]) { // Check if empty
  					$('a[href=#' + visible[0].attr('id') + ']').addClass('active');
  	      }
  			}
  		});
  
  		return selector;
  	};
  
  	/**
  	 * Listen for window resize events
  	 * @param {Object=} options						Optional. Set { throttle: number } to change throttling. Default: 100 ms
  	 * @returns {jQuery}		$(window)
  	 */
  	$.winSizeSpy = function(options) {
  		$.winSizeSpy = function() { return jWindow; }; // lock from multiple calls
  		options = options || {
  			throttle: 100
  		};
  		return jWindow.on('resize', throttle(onWinSize, options.throttle || 100));
  	};
  
  	/**
  	 * Enables ScrollSpy on a collection of elements
  	 * e.g. $('.scrollSpy').scrollSpy()
  	 * @param {Object=} options	Optional.
  											throttle : number -> scrollspy throttling. Default: 100 ms
  											offsetTop : number -> offset from top. Default: 0
  											offsetRight : number -> offset from right. Default: 0
  											offsetBottom : number -> offset from bottom. Default: 0
  											offsetLeft : number -> offset from left. Default: 0
  	 * @returns {jQuery}
  	 */
  	$.fn.scrollSpy = function(options) {
  		return $.scrollSpy($(this), options);
  	};
  
  })(jQuery);