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