UNPKG

scroll-stash

Version:

A JavaScript plugin to help preserve an element's scroll position.

3 lines (2 loc) 3.87 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t||self).ScrollStash=e()}(this,function(){function t(){return(t=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}var e={autoInit:!1,dataScroll:"scroll-stash",dataAnchor:"scroll-stash-anchor",selectorAnchor:"",selectorAnchorParent:"",selectorTopElem:"",selectorBotElem:"",alignment:"nearest",behavior:"auto",anchorPadding:16,saveKey:"ScrollStash",throttleDelay:100,customEventPrefix:"scroll-stash:"},r=function(t,e,r){var n=r.anchorPadding;if(r.selectorTopElem){var o=t.querySelector(r.selectorTopElem);o&&(n+=o.offsetHeight)}return e.offsetTop-n},n=function(t,e,r){var n=r.anchorPadding;if(r.selectorBotElem){var o=t.querySelector(r.selectorBotElem);o&&(n+=o.offsetHeight)}return e.offsetTop-(t.offsetHeight-(e.offsetHeight+n))},o=function(t,e){var r=t.getAttribute("data-"+e.dataAnchor);if("false"==r||"ignore"==r)return null;if(r&&t.querySelector(r))return t.querySelector(r);var n=e.selectorAnchor?t.querySelector(e.selectorAnchor):null;if(n&&e.selectorAnchorParent){var o=n.closest(e.selectorAnchorParent);if(o)return o}return n||null},s=function(t,e,s){var i=o(t,s);if(i){var a=function(t,e,o){var s=function(t,e,o){var s=r(t,e,o),i=n(t,e,o);return!(t.scrollTop>s||t.scrollTop<i)}(t,e,o);switch(o.alignment){case"start":return!s&&r(t,e,o);case"center":return!s&&function(t,e,o){var s=r(t,e,o),i=n(t,e,o);return i+(s-i)/2}(t,e,o);case"end":return!s&&n(t,e,o);case"nearest":return function(t,e,o){var s=r(t,e,o),i=n(t,e,o);return t.scrollTop>s?s:t.scrollTop<i&&i}(t,e,o);default:return!1}}(t,i,s);return a?(t.scroll({top:a,behavior:e=e||s.behavior}),t.dispatchEvent(new CustomEvent(s.customEventPrefix+"anchor",{bubbles:!0,detail:{scrolled:{value:a,behavior:e},key:t.getAttribute("data-"+s.dataScroll)}})),{scrolled:{value:a,behavior:e},msg:"Anchor was scrolled into view"}):{scrolled:!1,msg:"Anchor is already in view"}}return{scrolled:!1,msg:"Anchor was not found"}},i=function(t){var e={};return document.querySelectorAll("[data-"+t.dataScroll+"]").forEach(function(r){var n=r.getAttribute("data-"+t.dataScroll);n&&(e[n]=r.scrollTop)}),localStorage.setItem(t.saveKey,JSON.stringify(e)),document.dispatchEvent(new CustomEvent(t.customEventPrefix+"saved",{bubbles:!0,detail:{state:e}})),e};return function(){function r(r){this.settings=t({},e,r),this.state={},this.scrolls=[],this.ticking=!1,this.__handler=this.handler.bind(this),this.settings.autoInit&&this.init()}var n=r.prototype;return n.init=function(e){var r=this;void 0===e&&(e=null),e&&(this.settings=t({},this.settings,e)),this.state=function(t){if(localStorage.getItem(t.saveKey)){var e=JSON.parse(localStorage.getItem(t.saveKey));return Object.keys(e).forEach(function(r){var n=document.querySelector("[data-"+t.dataScroll+'="'+r+'"]');n&&(n.scrollTop=e[r])}),document.dispatchEvent(new CustomEvent(t.customEventPrefix+"applied",{bubbles:!0,detail:{state:e}})),e}return{}}(this.settings),this.state=Object.keys(this.state).length?this.state:i(this.settings),this.scrolls=document.querySelectorAll("[data-"+this.settings.dataScroll+"]"),this.scrolls.forEach(function(t){t.addEventListener("scroll",r.__handler,!1),s(t,!1,r.settings)})},n.destroy=function(){var t=this;this.scrolls.forEach(function(e){e.removeEventListener("scroll",t.__handler,!1)}),this.state={},this.scrolls=[],localStorage.removeItem(this.settings.saveKey)},n.handler=function(){var t=this;this.ticking||(this.ticking=!0,setTimeout(function(){t.state=i(t.settings),t.ticking=!1},this.settings.throttleDelay))},n.anchorGet=function(t){return o(t,this.settings)},n.anchorShow=function(t,e){return s(t,e,this.settings)},r}()}); //# sourceMappingURL=scripts.umd.js.map