javascript 回到顶部效果的实现代码_javascript技巧

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

复制代码代码如下:

//speed 滚动速度 time 时间间隔
function gotoTop(speed,time){

speed = speed || 0.1;
time = time || 16;

// 滚动条到页面顶部的水平距离
var x = document.body.scrollLeft;

// 滚动条到页面顶部的垂直距离
var y = document.body.scrollTop;

// 滚动距离 = 目前距离 / 速度, 因为距离原来越小, 速度是大于 1 的数, 所以滚动距离会越来越小
speed++;
window.scrollTo(Math.floor(x / speed), Math.floor(y / speed));

// 如果距离不为零, 继续调用迭代本函数
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!