javascripthtmljquerycss

How to slow down scroll to top speed?


I have the following simple scrollTop() function obtained from w3schools. The issue i have is setting the time for scrolling. Different people gave different methods and everyone removed one or all lines from the following code. I'm waiting for a function which can be added to set the scrolling speed and no other text is to be removed. Here's the codepen work https://codepen.io/vkdatta27/pen/zYqQbmM

var mybutton = document.getElementById("myBtn");

window.onscroll = function() {
  scrollFunction()
};

function scrollFunction() {
  if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
    mybutton.style.display = "block";
  } else {
    mybutton.style.display = "none";
  }
}

// When the user clicks on the button, scroll to the top of the document
function topFunction() {
  document.body.scrollTop = 0;
  document.documentElement.scrollTop = 0;
}
body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 20px;
}
#myBtn {
  display: none;
  position: fixed;
  bottom: 20px;
  right: 30px;
  z-index: 99;
  font-size: 18px;
  border: none;
  outline: none;
  background-color: red;
  color: white;
  cursor: pointer;
  padding: 15px;
  border-radius: 4px;
}
#myBtn:hover {
  background-color: #555;
}
html {
  scroll-behavior: smooth
}
<button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
<div style="background-color:black;color:white;padding:30px">Scroll Down</div>
<div style="background-color:lightgrey;padding:30px 30px 2500px">This example demonstrates how to create a "scroll to top" button that becomes visible
  <strong>when the user starts to scroll the page</strong></div>


Solution

  • Here is a pure Javascript solution. you may need to remove scroll-behavior: smooth style as this interrupts slow scrolling. in javascript scrollTo function provide the second parameters in milliseconds and function will take that much time to scroll to top.

    JS code referred from the answer @ https://stackoverflow.com/a/23844067

    var mybutton = document.getElementById("myBtn");
    window.onscroll = function() {
      scrollFunction()
    };
    
    function scrollFunction() {
      if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
        mybutton.style.display = "block";
      } else {
        mybutton.style.display = "none";
      }
    }
    // Bind your button click, scroll direction and effect speed
    document.getElementById("myBtn").onclick = function() {
      scrollTo(0, 8000); // it will take 8 seconds to reach to top.
    
    }
    
    // Element to move, time in ms to animate
    function scrollTo(element, duration) {
      var e = document.documentElement;
      if (e.scrollTop === 0) {
        var t = e.scrollTop;
        ++e.scrollTop;
        e = t + 1 === e.scrollTop-- ? e : document.body;
      }
      scrollToC(e, e.scrollTop, element, duration);
    }
    
    // Element to move, element or px from, element or px to, time in ms to animate
    function scrollToC(element, from, to, duration) {
      if (duration <= 0) return;
      if (typeof from === "object") from = from.offsetTop;
      if (typeof to === "object") to = to.offsetTop;
    
      scrollToX(element, from, to, 0, 1 / duration, 20, easeOutCuaic);
    }
    
    function scrollToX(element, xFrom, xTo, t01, speed, step, motion) {
      if (t01 < 0 || t01 > 1 || speed <= 0) {
        element.scrollTop = xTo;
        return;
      }
      element.scrollTop = xFrom - (xFrom - xTo) * motion(t01);
      t01 += speed * step;
      debugger;
      setTimeout(function() {
        scrollToX(element, xFrom, xTo, t01, speed, step, motion);
      }, step);
    }
    
    function easeOutCuaic(t) {
      t--;
      return t * t * t + 1;
    }
    body {
      font-family: Arial, Helvetica, sans-serif;
      font-size: 20px;
    }
    
    #myBtn {
      display: none;
      position: fixed;
      bottom: 20px;
      right: 30px;
      z-index: 99;
      font-size: 18px;
      border: none;
      outline: none;
      background-color: red;
      color: white;
      cursor: pointer;
      padding: 15px;
      border-radius: 4px;
    }
    
    #myBtn:hover {
      background-color: #555;
    }
    <button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
    <div style="background-color:black;color:white;padding:30px">Scroll Down</div>
    <div style="background-color:lightgrey;padding:30px 30px 2500px">This example demonstrates how to create a "scroll to top" button that becomes visible
      <strong>when the user starts to scroll the page</strong></div>