jquery.nav.js 6.6 KB

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