网络编程
位置:首页>> 网络编程>> JavaScript>> 点选TOP后并不是直接跳到页顶的,而是滚动上去

点选TOP后并不是直接跳到页顶的,而是滚动上去

  发布时间:2023-09-07 02:36:43 

标签:TOP,页顶,滚动实现

demo中的p中的script改大或改小看看。。。移动到top的时间始终是一定的,你也可以设置一个阀值,在页面高度到达这个阀值之前,移动的总时间是多少,当页面高度超过这个数时,移动的总时间反而以一定比例减小,自己试试看吧...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>回到页顶效果代码</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <meta name="Author" content="Sheneyan" /> <script> function s2top(){ var a = document.getElementsByTagName('a'); var l = a.length; for (var i = 0 ; i < l ; i ++){ var ai = a[i]; if (/#header$/.test(ai.href)){ ai.onclick = function(){ var o = this; var ot = o.offsetTop; for (var t = 0 ; t < 20 ; t ++){ setTimeout("scrollBy(0,-" + parseInt(ot / 20) + ")",t * 50 + 1); } return false; } } } } </script> <body onload="s2top()"> </body> <p> <script> for (var i = 0 ; i < 1000 ; i ++) document.write(i+""); </script> </p> go to 头 </body>


0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com