From 645f9ea00003ca67b50f5f9f712dbec4c6b78987 Mon Sep 17 00:00:00 2001 From: Leonardo Santos Date: Tue, 14 Nov 2017 21:25:50 -0200 Subject: [PATCH] Added `enableLocationHash` option --- README.md | 17 +++++++++-------- dist/menuspy.js | 21 ++++++++++++++------- dist/menuspy.min.js | 2 +- src/menuspy.js | 21 ++++++++++++++------- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 1b8edf1..c00f443 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Include MenuSpy `MenuSpy` will be available in the global scope. -Or install with NPM and require as a module +Or install via NPM/Yarn and require as a module ``` npm install menuspy @@ -57,10 +57,11 @@ The `MenuSpy()` constructor accepts two arguments: the container element and an ## Options -| Option | Type | Default | Description | -| ------------------ | -------- | ----------------------------------- | ------------------------------------------------------------------------ | -| `menuItemSelector` | String | `a[href^="#"]` | Menu items selector. | -| `activeClass` | String | `active` | Class applied on menu item relative to the currently visible section. | -| `threshold` | Integer | `15` | Ammount of space between your menu and the next section to be activated. | -| `hashTimeout` | Integer | `600` | Timeout to apply browser's hash location. | -| `callback` | Function | `function(currentItem) {}` | A function to be called every time a new menu item activates. | \ No newline at end of file +| Option | Type | Default | Description | +| ---------------------| -------- | -------------------------- | ------------------------------------------------------------------------ | +| `menuItemSelector` | String | `a[href^="#"]` | Menu items selector. | +| `activeClass` | String | `active` | Class applied on menu item relative to the currently visible section. | +| `threshold` | Integer | `15` | Ammount of space between your menu and the next section to be activated. | +| `enableLocationHash` | Boolean | `true` | Enable or disable browser's hash location change. | +| `hashTimeout` | Integer | `600` | Timeout to apply browser's hash location. | +| `callback` | Function | `function(currentItem) {}` | A function to be called every time a new menu item activates. | \ No newline at end of file diff --git a/dist/menuspy.js b/dist/menuspy.js index e9a49ed..0846d0b 100644 --- a/dist/menuspy.js +++ b/dist/menuspy.js @@ -75,11 +75,12 @@ var MenuSpy = function MenuSpy(element, options) { } var defaults = { - menuItemSelector: 'a[href^="#"]', - activeClass : 'active', - threshold : 15, - hashTimeout : 600, - callback : null + menuItemSelector : 'a[href^="#"]', + activeClass : 'active', + threshold : 15, + enableLocationHash : true, + hashTimeout : 600, + callback : null }; this.element = element; @@ -138,7 +139,11 @@ MenuSpy.prototype.activateItem = function activateItem (inViewElm) { if (!inViewElm) { this.scrollItems.forEach(function (item) { return utils.removeClass(item.elm.parentNode, activeClass); }); this.lastInViewElm = null; - this.debouncedHashFn(); + + if (this.enableLocationHash) { + this.debouncedHashFn(); + } + return; } @@ -155,7 +160,9 @@ MenuSpy.prototype.activateItem = function activateItem (inViewElm) { callback.call(this$1, item); } - this$1.debouncedHashFn(); + if (this$1.enableLocationHash) { + this$1.debouncedHashFn(); + } } }); } diff --git a/dist/menuspy.min.js b/dist/menuspy.min.js index 949f8be..426d08b 100644 --- a/dist/menuspy.min.js +++ b/dist/menuspy.min.js @@ -1,2 +1,2 @@ /*! MenuSpy v1.1.3 (Nov 14 2017) - http://leocs.me/menuspy/ - Copyright (c) 2017 Leonardo Santos; MIT License */ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.MenuSpy=e()}(this,function(){"use strict";var t={extend:function(t,e){for(var s in e)e.hasOwnProperty(s)&&(t[s]=e[s]);return t},offset:function(t){var e=t.getBoundingClientRect();return{top:e.top+window.pageYOffset,left:e.left+window.pageXOffset}},scrollTop:function(){return window.pageYOffset||document.documentElement.scrollTop},addClass:function(t,e){if(t.classList)t.classList.add(e);else{var s=t.className.split(" ");-1===s.indexOf(e)&&s.push(e),t.className=s.join(" ")}},removeClass:function(t,e){t.classList?t.classList.remove(e):t.className=t.className.replace(new RegExp("(^|\\b)"+e.split(" ").join("|")+"(\\b|$)","gi")," ")},debounce:function(t,e){var s=null;return function(){var n=arguments,o=this;s||(s=setTimeout(function(){return s=0,t.apply(o,n)},e))}}},e=function(e,s){var n=this;if(e){var o={menuItemSelector:'a[href^="#"]',activeClass:"active",threshold:15,hashTimeout:600,callback:null};this.element=e,this.options=t.extend(o,s),this.assignValues(),window.addEventListener("resize",t.debounce(function(){return n.assignValues()})),this.debouncedHashFn=t.debounce(function(){var e=n.lastInViewElm?"#"+n.lastInViewElm.id:"#";if(history.replaceState)history.replaceState(null,null,e);else{var s=t.scrollTop();window.location.hash=e,window.scrollTo(0,s)}},this.options.hashTimeout),this.cacheItems(),this.scrollFn()}};return e.prototype.assignValues=function(){this.currScrollTop=0,this.lastInViewElm=null,this.menuHeight=this.element.offsetHeight+this.options.threshold,this.menuItems=[].slice.call(this.element.querySelectorAll(this.options.menuItemSelector))},e.prototype.cacheItems=function(){this.scrollItems=this.menuItems.map(function(e){var s=e.dataset.target?document.querySelector(e.dataset.target):document.getElementById(e.hash.slice(1));if(s){return{elm:e,target:s,offset:t.offset(s).top}}return!1}),this.scrollItems=this.scrollItems.filter(Boolean)},e.prototype.tick=function(){var t=this.currScrollTop+this.menuHeight,e=this.scrollItems.filter(function(e){return e.offset utils.removeClass(item.elm.parentNode, activeClass)); this.lastInViewElm = null; - this.debouncedHashFn(); + + if (this.enableLocationHash) { + this.debouncedHashFn(); + } + return; } @@ -83,7 +88,9 @@ class MenuSpy { callback.call(this, item); } - this.debouncedHashFn(); + if (this.enableLocationHash) { + this.debouncedHashFn(); + } } }); }