UNPKG

1.1 kBJavaScriptView Raw
1/**
2 scrolldir - Vertical scroll direction in CSS
3 @version v1.5.0
4 @link https://github.com/dollarshaveclub/scrolldir.git
5 @author Patrick Fisher <patrick@pwfisher.com>
6 @license MIT
7**/
8!function(t){"function"==typeof define&&define.amd?define(t):t()}(function(){"use strict";var a,d,t,s="data-scrolldir",f="down",l=64,u=document.documentElement,c=window,m=document.body,h=32,v=512,w=Array(h),b=0;function e(){var t=c.scrollY||c.pageYOffset,e=a.timeStamp,n="down"===f?Math.max:Math.min,i=m.scrollHeight-c.innerHeight;if(t=Math.max(0,t),t=Math.min(i,t),w.unshift({y:t,t:e}),w.pop(),t===n(d,t))return b=e,void(d=t);var r=e-v;if(b<r){d=t;for(var o=0;o<h&&(w[o]&&!(w[o].t<r));o+=1)d=n(d,w[o].y)}Math.abs(t-d)>l&&(d=t,b=e,f="down"===f?"up":"down",u.setAttribute(s,f))}function n(t){return a=t,c.requestAnimationFrame(e)}t&&(t.attribute&&(s=t.attribute),t.el&&(u=t.el),t.win&&(c=t.win),t.dir&&(f=t.dir),t.thresholdPixels&&(l=t.thresholdPixels),!0===t.off)?(u.setAttribute(s,"off"),c.removeEventListener("scroll",n)):(d=c.scrollY||c.pageYOffset,u.setAttribute(s,f),c.addEventListener("scroll",n))});