Javascript returns to the implementation code for the top effect

  • 2020-03-30 01:45:30
  • OfStack


//Speed scrolling speed time interval
function gotoTop(speed,time){
    speed = speed || 0.1;
    time = time || 16;
    //The horizontal distance from the scroll bar to the top of the page
    var x = document.body.scrollLeft;
    //The vertical distance from the scroll bar to the top of the page
    var y = document.body.scrollTop;
    //The rolling distance is equal to the current distance/velocity, because the smaller the distance, the velocity is greater than 1, so the rolling distance is going to get smaller and smaller
    speed++; 
    window.scrollTo(Math.floor(x / speed), Math.floor(y / speed));
    //If the distance is not zero, continue to call the iterative function
    if(x > 0 || y > 0) {
        window.setTimeout("gotoTop(" + speed + ", " + time + ")", time);
    }
}


Related articles: