title: Stop script if users scrolls
body: <p>Disclaimer: I am not a JavaScript developer, I'm a web designer. HTML and CSS, I handle all day, JS, not so much. That's why I'm reaching out for help.</p>
  <p>The following script allows for a smooth scroll to the top of the page:</p>
  <pre><code>function scrollToTop() {
      var position =
          document.body.scrollTop || document.documentElement.scrollTop;
      if (position) {
          window.scrollBy(0, -Math.max(1, Math.floor(position / 10)));
          scrollAnimation = setTimeout(&quot;scrollToTop()&quot;, 30);
      } else clearTimeout(scrollAnimation);
  }
  </code></pre>
  <p>Is there a way to &quot;stop&quot; the script from executing if the user decides to scroll back down the moment the script is running and taking the user back to the top of the page?</p>
  <p>Here's a demo for reference: <a href="https://codepen.io/ricardozea/pen/ewBzyO" rel="nofollow noreferrer">https://codepen.io/ricardozea/pen/ewBzyO</a></p>
  <p>Thank you.</p>