Home  >  Article  >  Web Front-end  >  javascript return to top effect implementation code_javascript skills

javascript return to top effect implementation code_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:59:491009browse

Copy code The code is as follows:

//speed scroll speed time 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;

// 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);
     }
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn