vue-anchor-navigate
Version:
Vue.js anchor navigate plugin
2 lines • 4.65 kB
JavaScript
module.exports=function(t){var e={};function o(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,o),i.l=!0,i.exports}return o.m=t,o.c=e,o.d=function(t,e,n){o.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},o.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},o.t=function(t,e){if(1&e&&(t=o(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(o.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)o.d(n,i,function(e){return t[e]}.bind(null,i));return n},o.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return o.d(e,"a",e),e},o.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},o.p="",o(o.s=0)}([function(t,e,o){"use strict";var n=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(e,"__esModule",{value:!0});const i=n(o(1)),s=n(o(2)),r=n(o(3)),d=n(o(4)),c=n(o(5)),u=o(6);function l(t,e){return e+t.offsetHeight}e.default=new class{constructor(){this._anchorList=[],this._windowPositionTop=0,this._intersectionAnchor=void 0}install(t,e){this._bindEvents(),t.directive("anchor",{bind:t=>{const e=i.default();!function(t,e){t.setAttribute(u.ANCHOR_ATTRIBUTE_ID_NAME,e)}(t,e),c.default(t).then(()=>{this._addAnchor(t,e)}).catch(t=>{console.log(t)})},update:t=>{console.log("update",t)},unbind:t=>{const e=t.getAttribute(u.ANCHOR_ATTRIBUTE_ID_NAME);null!==e&&(this._removeAnchor(e),function(t){t.removeAttribute(u.ANCHOR_ATTRIBUTE_ID_NAME)}(t))}})}_bindEvents(){window.addEventListener("DOMContentLoaded",this._DOMContentLoaded.bind(this)),d.default({target:window,event:"scroll",method:this._windowScroll.bind(this),delay:u.WINDOW_SCROLL_DELAY}),d.default({target:window,event:"resize",method:this._windowResize.bind(this),delay:u.WINDOW_RESIZE_DELAY})}_unbindEvents(){window.removeEventListener("DOMContentLoaded",this._DOMContentLoaded.bind(this)),window.removeEventListener("scroll",this._windowScroll.bind(this))}_DOMContentLoaded(){this._scrollToAnchorPosition()}_windowScroll(){this._setWindowPositionTop(),this._setIntersectionAnchor(),this._manageHash()}_windowResize(){this._updateAnchorsPositions()}_addAnchor(t,e){const o=s.default(t);this._anchorList.push({id:e,el:t,hash:"#"+t.id,positionTop:o,positionBottom:l(t,o)})}_removeAnchor(t){const e=this._anchorList.findIndex(e=>e.id===t);e>-1&&this._anchorList.splice(e,1)}_setWindowPositionTop(){this._windowPositionTop=window.scrollY}_setIntersectionAnchor(){const t=this._anchorList.filter(t=>this._isWinInAnchorPos(t));this._intersectionAnchor=t[0]}_isWinInAnchorPos(t){return this._windowPositionTop>t.positionTop&&this._windowPositionTop<t.positionBottom}_manageHash(){this._intersectionAnchor?r.default.set(this._intersectionAnchor.hash):r.default.clear()}_scrollToAnchorPosition(){const t=this._anchorList.find(t=>t.hash===r.default.get());t&&window.scrollTo(0,t.positionTop+1)}_updateAnchorsPositions(){this._anchorList.forEach((t,e)=>{const o=s.default(t.el),n=l(t.el,t.positionTop);this._anchorList[e].positionTop=o,this._anchorList[e].positionBottom=n})}}},function(t,e,o){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.default=()=>Math.random().toString(36).substr(2,5)},function(t,e,o){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.default=t=>{const e=t.getBoundingClientRect(),o=document.body,n=document.documentElement.clientTop||o.clientTop||0,i=e.top+window.scrollY-n-1;return Math.round(i)}},function(t,e,o){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.default={set(t){history.pushState(null,"",t)},clear(){window.history.replaceState("",document.title,window.location.href.replace(/#.*$/,""))},get:()=>window.location.hash}},function(t,e,o){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.default=function({target:t,event:e,method:o,delay:n}){let i;t.addEventListener(e,()=>{clearTimeout(i),i=setTimeout(o,n)})}},function(t,e,o){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.default=t=>new Promise(e=>{!function t(e,o){!function(t){return null!==t.offsetParent}(e)?setTimeout(()=>t(e,o),1):o()}(t,e)})},function(t,e,o){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.WINDOW_RESIZE_DELAY=e.WINDOW_SCROLL_DELAY=e.ANCHOR_ATTRIBUTE_ID_NAME=void 0,e.ANCHOR_ATTRIBUTE_ID_NAME="data-v-anchor-navigate-id",e.WINDOW_SCROLL_DELAY=50,e.WINDOW_RESIZE_DELAY=4}]);
//# sourceMappingURL=main.js.map