无限垂直卷轴

时间:2012-12-27 16:37:23

标签: javascript html

我有一个垂直滚动脚本,对我来说效果很好,但问题是它会在一段时间后停止。如何修改脚本以便它一次又一次地从上到下无限滚动?
这是代码:

<html>
<head>
<style type="text/css">
.frame {
font:12px arial;
width:200;
height:300;
border:none;
overflow: hidden;
border:1px solid black;
padding:5;
}
</style>
<script language="javascript">
i = 0
var speed = 1
function scroll() {
i = i + speed
var div = document.getElementById("news")
div.scrollTop = i
if (i > div.scrollHeight - 160) {i = 0}
t1=setTimeout("scroll()",100)
}
</script>
</head>
<body onload="scroll()">
<div class="frame" id="news" onmouseover="clearTimeout(t1)" onmouseout="scroll()" >
Some Text text 1<br>
Some Text text 2<br>
Some Text text 3<br>
Some Text text 4<br>
Some Text text 5<br>
Some Text text 6<br>
Some Text text 7<br>
Some Text text 8<br>
Some Text text 9<br>
Some Text text 10<br>
Some Text text 11<br>
Some Text text 12<br>
Some Text text 13<br>
Some Text text 14<br>
Some Text text 15<br>
Some Text text 16<br>
Some Text text 17<br>
Some Text text 18<br>
Some Text text 19<br>
Some Text text 20<br>
Some Text text 21<br>
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

它实际上并没有停止 尝试将if()条件更新为

if (i > div.scrollHeight - 300) {i = 0}

答案 1 :(得分:0)

你的问题是你只在scrollHeight减去160

之后重置它

它应该是scrollHeight - divHeight