一个js自动滚动代码问题,怎么让他执行一次就不执行了!

<script>
var timer, scrollHeight, viewHeight, step = 20, sTop = 0;
document.onclick = function () { clearInterval(timer); }
function Move() {
//设置滚动前获取当前的的滚动高度和sTop比较,如果小于sTop或者和sTop的差距大于step定义的,说明拖拽过滚动条了
var nowScrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
if (nowScrollTop < sTop || (nowScrollTop - top > step)) clearInterval(timer);
sTop += step;
document.documentElement.scrollTop = document.body.scrollTop = sTop;
if (sTop + viewHeight > scrollHeight) {//滚动到底部
clearInterval(timer);
document.documentElement.scrollTop = document.body.scrollTop = 0//跳转到顶部
}
}
window.onresize = function () {
viewHeight = document.body.clientHeight;
scrollHeight = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
}
window.onload = function () {
window.onresize();
timer = setInterval(Move, 100);
}
</script>

我想网页向下执行滚动一次就好,这段代码的连续滚动的。

if (sTop + viewHeight > scrollHeight) {//滚动到底部
clearInterval(timer);
//像我这样注释掉这行 document.documentElement.scrollTop = document.body.scrollTop = 0//跳转到顶部
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-07-21
这是典型的不兼容所致。追问

兼容好像没问题。
window.onload = function () {
window.onresize();
timer = setInterval(Move, 100);
}
这个是镉100毫秒执行一次,我想改成只滚动一次

追答

window.onload = function () {

window.onresize();
timer = setInterval(Move, 100);
clearInterval(timer);
}

第2个回答  2014-07-21
setInterval 改成setTimeout本回答被提问者采纳
相似回答