You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

135 lines
4.3 KiB

  1. /*!
  2. * NavScroll.js
  3. * Version: 1.2.0
  4. * Author: Jeroen Hammann
  5. *
  6. * Copyright (c) 2014 Jeroen Hammann
  7. * Released under the MIT license
  8. */
  9. ;(function ($, window, document, undefined) {
  10. 'use strict';
  11. var pluginName = 'navScroll',
  12. defaults = {
  13. // The time it takes to scroll to the element (set this to 0 so it obviously won't show an animation).
  14. scrollTime: 500,
  15. // The class of the items which invokes the scroll animation. All anchor tags inside the element are clickable when the value is empty.
  16. navItemClassName: '',
  17. // Set the height of the navigation (to use as offset). 'auto' let's the plugin determine the height automatically, a number determines the height in px.
  18. navHeight: 'auto',
  19. // If your navigation hides and is used as a dropdown on small screens setting this to true hides the dropdown after a click.
  20. mobileDropdown: false,
  21. // Additionaly you can insert the mobile nav's classname here, when left empty the plugin searches for a <ul> in the same parent element.
  22. mobileDropdownClassName: '',
  23. // The window width, which functions as a breakpoint between desktop and mobile.
  24. mobileBreakpoint: 1024,
  25. // Set to 'true' if you want to enable scrollspy.
  26. scrollSpy: false
  27. };
  28. function NavScroll(element, options) {
  29. this.element = element;
  30. this.options = $.extend({}, defaults, options);
  31. this._defaults = defaults;
  32. this._name = pluginName;
  33. this.init();
  34. }
  35. NavScroll.prototype = {
  36. init: function() {
  37. var self, options, element, navItem, navOffset, scrollTime;
  38. self = this;
  39. options = self.options;
  40. element = self.element;
  41. if (options.navItemClassName === '') {
  42. navItem = $(element).find('a');
  43. } else {
  44. navItem = $(element).find('.' + options.navItemClassName);
  45. }
  46. if (options.navHeight === 'auto') {
  47. navOffset = $(element).height();
  48. } else if (isNaN(options.navHeight)) {
  49. throw new Error ('\'navHeight\' only accepts \'auto\' or a number as value.');
  50. } else {
  51. navOffset = options.navHeight;
  52. }
  53. navItem.on('click', function(e){
  54. var url, parts, target, targetOffset, targetTop;
  55. url = this.href;
  56. parts = url.split('#');
  57. target = parts[1];
  58. if (target !== undefined) {
  59. e.preventDefault();
  60. targetOffset = $('#' + target).offset();
  61. targetTop = targetOffset.top;
  62. }
  63. if ($(this).data('scrolltime') !== undefined) {
  64. scrollTime = $(this).data('scrolltime');
  65. } else {
  66. scrollTime = options.scrollTime;
  67. }
  68. if (options.mobileDropdown && $(window).width() >= 0 && $(window).width() <= options.mobileBreakpoint) {
  69. if (options.mobileDropdownClassName === '') {
  70. $(element).find('ul').slideUp('fast');
  71. } else {
  72. $('.' + options.mobileDropdownClassName).slideUp('fast');
  73. }
  74. }
  75. $('html, body').stop().animate({
  76. scrollTop: targetTop - navOffset
  77. }, scrollTime);
  78. });
  79. if (options.scrollSpy) {
  80. var scrollItems;
  81. scrollItems = [];
  82. navItem.each(function() {
  83. var scrollItemId = $(this).attr('href');
  84. scrollItems.push($(scrollItemId));
  85. });
  86. $(window).on('scroll', function () {
  87. self.scrollspy(navItem, scrollItems);
  88. });
  89. self.scrollspy(navItem, scrollItems);
  90. }
  91. },
  92. scrollspy: function(navItem, scrollItems) {
  93. var scrollPos, changeBounds, i, l;
  94. scrollPos = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
  95. // changeBounds = window.innerHeight / 2 || document.documentElement.clientHeight / 2;
  96. l = navItem.length;
  97. for (i = 0; l > i; i++) {
  98. var item = scrollItems[i];
  99. if (scrollPos > (item.offset().top-60)) {
  100. navItem.removeClass('active');
  101. $(navItem[i]).addClass('active');
  102. }
  103. }
  104. }
  105. };
  106. //$.fn[pluginName] = function (options) {
  107. // return this.each(function () {
  108. // if (!$.data(this, 'plugin_' + pluginName)) {
  109. // $.data(this, 'plugin_' + pluginName,
  110. // new NavScroll(this, options));
  111. // }
  112. // });
  113. //};
  114. })(jQuery, window, document);