-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoneFrame.min.js
1 lines (1 loc) · 1.33 KB
/
oneFrame.min.js
1
function init(e){document.querySelector(e)?(oneFrame=document.querySelector(e),children=oneFrame.children,lengthChildren=children.length,events(oneFrame)):console.log(void 0)}function events(e){e.addEventListener("wheel",throttle(handler,1e3)),window.addEventListener("resize",debounce(handler,100))}function handler({deltaY:e,type:t}){if("wheel"===t){let t=calDirection(e);if("down"===t){if(!isChild(elementActive+1))return;elementActive+=1}else{if(!isChild(elementActive-1))return;elementActive-=1}lastDirection=t}oneFrame.style.transform=`translate3d(0px, -${calculateY(elementActive,lastDirection)}px, 0px)`}function isChild(e){return e>=0&&e<=lengthChildren-1}function calDirection(e){return 1===Math.sign(e)?"down":"up"}function calculateY(e,t){let n=calculateTop(e);if("down"===t){const t=document.documentElement.clientHeight,i=children[e].getBoundingClientRect().height;return n+i-t}if("up"===t)return n}function calculateTop(e){let t=0;for(let n=0;n<e;n++)t+=children[n].getBoundingClientRect().height;return t}function throttle(e,t){let n=!0;return function(...i){n&&(n=!1,e.apply(this,i),setTimeout(()=>n=!0,t))}}function debounce(e,t){let n;return function(){const i=this,l=arguments;clearTimeout(n),n=setTimeout(()=>e.apply(i,l),t)}}let oneFrame=void 0,children=void 0,lengthChildren=void 0,elementActive=0,lastDirection="up";export default{init:init};