16
2013
07

js控制滚动条示例

HTML和JS:

<html>
<head>
<script type="text/javascript">
var currentPosition,timer,timer1;
function GoTop(){
timer=setInterval("runToTop()",1);
}
function GoButtom(){
//alert(document.body.scrollHeight);
timer1=setInterval("runToButtom()",1);

}
function runToTop(){
currentPosition=document.documentElement.scrollTop || document.body.scrollTop;
currentPosition-=10;
if(currentPosition>0)
{
window.scrollTo(0,currentPosition);
}
else
{
window.scrollTo(0,0);
clearInterval(timer);
alert("over_top");
}
}
function runToButtom(){
currentPosition=document.documentElement.scrollTop || document.body.scrollTop;
currentPosition+=10;

var topheight=document.body.scrollHeight-document.body.clientHeight;
//alert(currentPosition+"---"+topheight);
if(currentPosition<topheight)
{
window.scrollTo(0,currentPosition);
}
else
{
window.scrollTo(0,topheight);
clearInterval(timer1);
alert("over_buttom");
}
}
</script>
<style type="text/css">
</style>
</head>
<body>
<div style="height:1000px;text-align:center;font-size:200px;font-weight:bold;">饭</div>
<div style="height:1000px;text-align:center;font-size:200px;font-weight:bold;">吃</div>
<div style="height:1000px;text-align:center;font-size:200px;font-weight:bold;">家</div>
<div style="height:1000px;text-align:center;font-size:200px;font-weight:bold;">回</div>
<div style="height:1000px;text-align:center;font-size:200px;font-weight:bold;">你</div>
<div style="height:1000px;text-align:center;font-size:200px;font-weight:bold;">带</div>
<div id="back-up" onclick="GoTop()" style="border:1px solid red;height:100px;width:15px;position:fixed;cursor:pointer;right:10px;bottom:30px;">返回顶部</div>
<script>
GoButtom();
</script>
</body>
</html>




版权声明:
作者:真爱无限 出处:http://www.pukuimin.top 本文为博主原创文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接.
« 上一篇下一篇 »

相关文章:

评论列表:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。