SoFunction
Updated on 2025-03-09

Javascript Back to top effect implementation code


//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 = ;

// Vertical distance from the scroll bar to the top of the page
    var y = ;

// Scroll distance = Current distance / speed, because the distance is smaller and the speed is greater than 1, so the scroll distance will become smaller and smaller
    speed++;
    ((x / speed), (y / speed));

// If the distance is not zero, continue to call the iterative function
    if(x > 0 || y > 0) {

        ("gotoTop(" + speed + ", " + time + ")", time);
    }
}