UNPKG

scrolldir

Version:
9 lines (8 loc) 1.31 kB
/** scrolldir - Vertical scroll direction in CSS @version v1.5.3 @link https://github.com/yowainwright/scrolldir.git @author Patrick Fisher <patrick@pwfisher.com>, Jeffry Wainwright <yowainwright@gmail.com> @license MIT **/ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).scrollDir=e()}(this,function(){"use strict";var s,d,f="data-scrolldir",l="down",a=64,u=document.documentElement,c=window,m=document.body,h=32,p=512,b=Array(h),v=0;function e(){var t=c.scrollY||c.pageYOffset,e=s.timeStamp,n="down"===l?Math.max:Math.min,i=m.scrollHeight-c.innerHeight;if(t=Math.max(0,t),t=Math.min(i,t),b.unshift({y:t,t:e}),b.pop(),t===n(d,t))return v=e,void(d=t);var o=e-p;if(v<o){d=t;for(var r=0;r<h&&(b[r]&&!(b[r].t<o));r+=1)d=n(d,b[r].y)}Math.abs(t-d)>a&&(d=t,v=e,l="down"===l?"up":"down",u.setAttribute(f,l))}function n(t){return s=t,c.requestAnimationFrame(e)}return function(t){return t&&(t.attribute&&(f=t.attribute),t.el&&(u=t.el),t.win&&(c=t.win),t.dir&&(l=t.dir),t.thresholdPixels&&(a=t.thresholdPixels),!0===t.off)?(u.setAttribute(f,"off"),c.removeEventListener("scroll",n)):(d=c.scrollY||c.pageYOffset,u.setAttribute(f,l),c.addEventListener("scroll",n))}});