Start onscroll function when specific element is in viewport / not on topof the page - jsfiddle inside

生来就可爱ヽ(ⅴ<●) 提交于 2020-12-15 06:24:12

问题


I made this parallax scrolling.

Using tweenmax library.

Right now scrolling is calculated for whole page, so by the time I reach parallax elements, they moved a lot, I need to start calculating scrolling only after parent #wrap comes to viewport.

HTML :

<div class="asd"></div>
<div id="wrap">
  <div class="scroll-parallax-1 a"></div>
  <div class="scroll-parallax-2 b"></div>
  <div class="scroll-parallax-3 c"></div>
  <div class="scroll-parallax-4 d"></div>
  <div class="scroll-parallax-5 e"></div>
</div>

Script :

document.onscroll = function(){
  console.log(0.8*scrollY);
  TweenMax.set(".scroll-parallax-1", {y: 0.1*-scrollY });
  TweenMax.set(".scroll-parallax-2", {y: 0.2*-scrollY });
  TweenMax.set(".scroll-parallax-3", {y: 0.4*-scrollY });
  TweenMax.set(".scroll-parallax-4", {y: 0.7*-scrollY });
  TweenMax.set(".scroll-parallax-5", {y: 1.2*-scrollY });
}

Full jsfiddle here

As you can see, by the time i #wrap comes to view, elements are already in the grey .asd div

Any ideas?

来源:https://stackoverflow.com/questions/64134341/start-onscroll-function-when-specific-element-is-in-viewport-not-on-topof-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!