PageSourceSearch

https://macgence.com/wp-content/plugins/top-table-of-contents/public/js/jquery.nav.js?ver=1.4.1

js macgence.com collected 2026-10-02 12:06:14 UTC 5,212 bytes, 224 lines download raw bytes

1/*
2 * jQuery One Page Nav Plugin
3 * http://github.com/davist11/jQuery-One-Page-Nav
4 *
5 * Copyright (c) 2010 Trevor Davis (http://trevordavis.net)
6 * Dual licensed under the MIT and GPL licenses.
7 * Uses the same license as jQuery, see:
8 * http://jquery.org/license
9 *
10 * @version 3.0.0
11 *
12 * Example usage:
13 * $('#nav').onePageNav({
14 *   currentClass: 'current',
15 *   changeHash: false,
16 *   scrollSpeed: 750
17 * });
18 */
19
20;(function($, window, document, undefined){
21
22	// our plugin constructor
23	var OnePageNav = function(elem, options){
24		this.elem = elem;
25		this.$elem = $(elem);
26		this.options = options;
27		this.metadata = this.$elem.data('plugin-options');
28		this.$win = $(window);
29		this.sections = {};
30		this.didScroll = false;
31		this.$doc = $(document);
32		this.docHeight = this.$doc.height();
33	};
34	
35	let offset_from_top = page_nav.offset_from_top;
36
37	// the plugin prototype
38	OnePageNav.prototype = {
39		defaults: {
40			navItems: 'a',
41			currentClass: 'current',
42			changeHash: false,
43			easing: 'swing',
44			filter: '',
45			scrollSpeed: 750,
46			scrollThreshold: 0.5,
47			begin: false,
48			end: false,
49			scrollChange: false
50		},
51
52		init: function() {
53			// Introduce defaults that can be extended either
54			// globally or using an object literal.
55			this.config = $.extend({}, this.defaults, this.options, this.metadata);
56
57			this.$nav = this.$elem.find(this.config.navItems);
58
59			//Filter any links out of the nav
60			if(this.config.filter !== '') {
61				this.$nav = this.$nav.filter(this.config.filter);
62			}
63
64			//Handle clicks on the nav
65			this.$nav.on('click.onePageNav', $.proxy(this.handleClick, this));
66
67			//Get the section positions
68			this.getPositions();
69
70			//Handle scroll changes
71			this.bindInterval();
72
73			//Update the positions on resize too
74			this.$win.on('resize.onePageNav', $.proxy(this.getPositions, this));
75
76			return this;
77		},
78
79		adjustNav: function(self, $parent) {
80			self.$elem.find('.' + self.config.currentClass).removeClass(self.config.currentClass);
81			$parent.addClass(self.config.currentClass);
82		},
83
84		bindInterval: function() {
85			var self = this;
86			var docHeight;
87
88			self.$win.on('scroll.onePageNav', function() {
89				self.didScroll = true;
90			});
91
92			self.t = setInterval(function() {
93				docHeight = self.$doc.height();
94
95				//If it was scrolled
96				if(self.didScroll) {
97					self.didScroll = false;
98					self.scrollChange();
99				}
100
101				//If the document height changes
102				if(docHeight !== self.docHeight) {
103					self.docHeight = docHeight;
104					self.getPositions();
105				}
106			}, 250);
107		},
108
109		getHash: function($link) {
110			return $link.attr('href').split('#')[1];
111		},
112
113		getPositions: function() {
114			var self = this;
115			var linkHref;
116			var topPos;
117			var $target;
118
119			self.$nav.each(function() {
120				linkHref = self.getHash($(this));
121				$target = $('#' + linkHref);
122
123				if($target.length) {
124					topPos = $target.offset().top;
125					self.sections[linkHref] = Math.round(topPos);
126				}
127			});
128		},
129
130		getSection: function(windowPos) {
131			var returnValue = null;
132			var windowHeight = Math.round(this.$win.height() * this.config.scrollThreshold);
133
134			for(var section in this.sections) {
135				if((this.sections[section] - windowHeight) < windowPos) {
136					returnValue = section;
137				}
138			}
139
140			return returnValue;
141		},
142
143		handleClick: function(e) {
144			var self = this;
145			var $link = $(e.currentTarget);
146			var $parent = $link.parent();
147			var newLoc = '#' + self.getHash($link);
148
149			if(!$parent.hasClass(self.config.currentClass)) {
150				//Start callback
151				if(self.config.begin) {
152					self.config.begin();
153				}
154
155				//Change the highlighted nav item
156				self.adjustNav(self, $parent);
157
158				//Removing the auto-adjust on scroll
159				self.unbindInterval();
160
161				//Scroll to the correct position
162				self.scrollTo(newLoc, function() {
163					//Do we need to change the hash?
164					if(self.config.changeHash) {
165						window.location.hash = newLoc;
166					}
167
168					//Add the auto-adjust on scroll back in
169					self.bindInterval();
170
171					//End callback
172					if(self.config.end) {
173						self.config.end();
174					}
175				});
176			}
177
178			e.preventDefault();
179		},
180
181		scrollChange: function() {
182			var windowTop = this.$win.scrollTop() - 250;
183			var position = this.getSection(windowTop);
184			var $parent;
185
186			//If the position is set
187			if(position !== null) {
188				$parent = this.$elem.find('a[href$="#' + position + '"]').parent();
189
190				//If it's not already the current section
191				if(!$parent.hasClass(this.config.currentClass)) {
192					//Change the highlighted nav item
193					this.adjustNav(this, $parent);
194
195					//If there is a scrollChange callback
196					if(this.config.scrollChange) {
197						this.config.scrollChange($parent);
198					}
199				}
200			}
201		},
202		scrollTo: function(target, callback) {
203			var offset = $(target).offset().top - offset_from_top;
204
205			$('html, body').animate({
206				scrollTop: offset
207			}, this.config.scrollSpeed, this.config.easing, callback);
208		},
209
210		unbindInterval: function() {
211			clearInterval(this.t);
212			this.$win.unbind('scroll.onePageNav');
213		}
214	};
215
216	OnePageNav.defaults = OnePageNav.prototype.defaults;
217
218	$.fn.onePageNav = function(options) {
219		return this.each(function() {
220			new OnePageNav(this, options).init();
221		});
222	};
223
224})( jQuery, window , document );

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.