-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmlp.min.js
1 lines (1 loc) · 1.76 KB
/
smlp.min.js
1
class Smlp{constructor(a,b,c){this.parent=document.getElementById(a),this.children=this.parent.getElementsByClassName(b),this.parallaxSettings=c,this.showStates=!1,this.resizeThrottleDelay=50,this.resizeCallDate=0,this.resizeTimeoutDone=!0,window.requestAnimationFrame=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.msRequestAnimationFrame}update(){let a=Array.from(this.children).map(a=>a.getBoundingClientRect());this.parallaxContentHeight=Math.max.apply(null,a.map(a=>a.height)),this.parallaxContentPosY=Math.min.apply(null,a.map(a=>a.top))}init(){this.update(),window.addEventListener("resize",()=>{let a=Date.now();this.resizeCallDate+this.resizeThrottleDelay<a?(this.resizeCallDate=a,this.update()):(this.resizeCallDate=a,this.resizeTimeoutDone&&(this.resizeTimeoutDone=!1,window.setTimeout(()=>{this.update(),this.resizeTimeoutDone=!0},this.resizeThrottleDelay)))}),window.addEventListener("scroll",this.animate.bind(this),!1)}currentSettings(){let a=window.innerWidth,b={slowdown:0};for(let c of this.parallaxSettings)if(c.screen<a||"undefined"==typeof c.screen)for(let a in c)b[a]=c[a];return b}animate(){let a=this.currentSettings(),b=a.start*this.parallaxContentHeight,c=a.height*this.parallaxContentHeight,d=a.offset*this.parallaxContentHeight,e=(window.pageYOffset-b+this.parallaxContentPosY+window.innerHeight/2)/c;this.showStates&&(document.body.style.backgroundColor="blue"),0>e&&(this.showStates&&(document.body.style.backgroundColor="red"),e=0),1<e&&(this.showStates&&(document.body.style.backgroundColor="green"),e=1);let f=c*a.slowdown*e,g=d/this.children.length;window.requestAnimationFrame(()=>{for(let a=0;a<this.children.length;a++)this.children[a].style.transform="translateY("+(e*g*a+f-window.pageYOffset)+"px)"})}}