Home > Web Front-end > JS Tutorial > body text

javascript return to top effect implementation code_javascript skills

WBOY
Release: 2016-05-16 16:59:49
Original
1014 people have browsed it

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template