-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-scroll-hook.min.js
1 lines (1 loc) · 2 KB
/
simple-scroll-hook.min.js
1
!function(t,e){"use strict";"function"==typeof define&&define.amd?define(e):"object"==typeof exports&&"object"==typeof module?module.exports=e():t.scrollHook=e()}(this,function(){"use strict";function t(){this._events={},this._positions=[],this._throttleTime=50}return t.prototype.setThrottleTime=function(t){return this._throttleTime=t,this},t.prototype.register=function(t,e){e=e||{};var i=e.position||t.offsetTop,n=e.initialStates||[],o=e.finalStates||[];this._events[i]=this._events[i]||[],"string"==typeof n&&(n=[e.initialStates]),"string"==typeof o&&(o=[e.finalStates]);var s={HTMLel:t,initialStates:n,finalStates:o};return-1===this._positions.indexOf(i)&&this._positions.push(i),this._events[i].push(s),this},t.prototype.determineMin=function(){var t=this._positions;this.min=0===t.length?null:Math.min.apply(null,t)},t.prototype.transitionStates=function(t){for(var e=0;e<t.length;e++){for(var i=t[e],n=i.HTMLel.classList,o=0;o<i.initialStates.length;o++)n.remove(i.initialStates[o]);for(var s=0;s<i.finalStates.length;s++)n.add(i.finalStates[s])}},t.prototype.transitionElements=function(){for(var t,e,i=window.pageYOffset+window.innerHeight,n=this._positions.length,o=0;n>o;o++)t=this._positions[o],i>=t&&(e=this._events[t],this.transitionStates(e),delete this._events[t],this._positions.splice(o,1),o-=1,this.determineMin())},t.prototype.createThrottle=function(t){var e,i=this._throttleTime||50,n=!1;return function(){n||(n=!0,e=setTimeout(function(){window.dispatchEvent(new CustomEvent(t)),n=!1,clearTimeout(e)},i))}},t.prototype.start=function(){function t(){return e.min?void(window.pageYOffset+window.innerHeight<e.min||e.transitionElements()):(window.removeEventListener("optimizedScroll",t),void window.removeEventListener("scroll",i))}this.determineMin();var e=this,i=this.createThrottle("optimizedScroll");window.addEventListener("scroll",i),document.addEventListener("DOMContentLoaded",function n(){e.transitionElements(),document.removeEventListener("DOMContentLoaded",n)}),window.addEventListener("optimizedScroll",t)},new t});