title: How to get exact scroll position when animating elements onScroll
body: <p>I am trying to animate elements while scrolling based on the elements progress through the scene. I am running into the issue of the browser returning intermittent scroll positions making it difficult to be exact when starting and stopping element animation.</p>
  
  <p>Minimal example: </p>
  
  <p>Fiddle <a href="https://jsfiddle.net/wamosjk/s1ahmnp9/" rel="nofollow noreferrer">Fiddle Demo</a></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const tweens = document.querySelectorAll('.tween');
  const trigger = document.querySelector('.start');
  const triggerRect = trigger.getBoundingClientRect();
  
  let yScroll = window.scrollY;
  let yCurrent = yScroll;
  let AF = null;
  
  const start = triggerRect.top - yScroll - 200;
  const end = start + 400;
  
  const updateScroll = () =&gt; {
   yScroll = window.scrollY;
    startAnimation();
  }
  
  const startAnimation = () =&gt; {
    if(!AF) AF = requestAnimationFrame(animate)
  }
  
  const cancelAnimation = () =&gt; {
    yCurrent = yScroll;
    cancelAnimationFrame(AF);
    AF = null;
  }
  
  const animate = () =&gt; {
    if(yCurrent === yScroll) cancelAnimation();
    else {
     updateElements();
      yCurrent = yScroll;
      AF = requestAnimationFrame(animate);
    }
  }
  
  const updateElements = () =&gt; {
    const pos = yCurrent - start;
    if(inScene()){
      tweens[0].style.transform = `translateX(${pos}px)`;
      tweens[1].style.transform = `translateY(${pos}px)`;
    }
  }
  
  const inScene = () =&gt; {
    return start &lt;= yCurrent &amp;&amp; end &gt;= yCurrent ? true : false;
  };
  
  
  window.addEventListener('scroll', updateScroll);</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.wrapper{
    position:relative;
    margin: 100vh 20px;
    background: rgb(240,240,240);
    height: 900px;
    border: 1px solid red;
  }
  
  .line{
    position:absolute;
    left: 0;
    width: 100%;
    height: 1px;
    background: red;
  }
  
  .start{
    top: 200px;
  }
  
  .end{
    bottom: 200px;
  }
  
  .tween{
    position:absolute;
    top:200px;
    width: 100px;
    height: 100px;
    background: blue;
  }
  
  .left{
    left: 0;
  }
  
  .right{
    right: 0;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;h1&gt;Scroll Down&lt;/h1&gt;
  &lt;div class="wrapper"&gt;
    &lt;div class="line start trigger"&gt;&lt;/div&gt;
    &lt;div class="line end"&gt;&lt;/div&gt;
    &lt;div class="tween left"&gt;&lt;/div&gt;
    &lt;div class="tween right"&gt;&lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>As you can see the elements are supposed to stop on the lines but when you scroll they never really do.  And the faster you scroll the worse it becomes.</p>
  
  <p><strong>So is there a technique to either return the correct scroll position or somehow debounce the elements so then can reach the full range when scrolling instead of constantly coming up short of their intended position?</strong></p>
  
  <p>I know this is possible because scrolling libraries like ScrollMagic do a pretty good job of handling this but I don't really want to deconstruct then entire ScrollMagic framework to find out how they achieve it.  I would use the ScrollMagic framework itself but I am trying to have a momentum style scrolling container wrapping the page and translating it on scroll along with elements inside this container and using ScrollMagic it is pretty buggy.  So I figured I would post the question here and see if anyone had any experience or insight on the situation.</p>
  
  <p>Any guidance would be appreciated as I have been mulling over this for a while.</p>