Home > Article > Web Front-end > javascript return to top effect implementation code_javascript skills
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;
// Scroll distance = current distance/speed, because the smaller the distance is, the speed is a number greater than 1, so the scrolling distance will become smaller and smaller
speed ;
window.scrollTo(Math.floor( x / speed), Math.floor(y / speed));
// If the distance is not zero, continue to call this iteration function
if(x > 0 || y > 0) {
window.setTimeout("gotoTop(" speed ", " " time ")", time);
}
}