gpt4 book ai didi

保持网页滚动的Javascript,慢慢向下滚动,等待,回到顶部

转载 作者:搜寻专家 更新时间:2023-10-31 23:20:11 24 4
gpt4 key购买 nike

我想要在本地测验中显示的网页上需要一个 javascript。该页面显示了分数表,但是有 60 个团队,我无法一次显示所有。所以我想在缓慢向下滚动的页面中添加一个javascript,当页面处于底部时,它应该等待2秒然后跳回到页面顶部并再次开始向下滚动。

https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_scrollto我使用了这个脚本:

<!DOCTYPE html>
<html>
<head>
<style>
body {
width: 500px;
}
</style>
</head>
<body onLoad="pageScroll()">

<h1>Scores</h1>

test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>
test test test <br>

<script>

function pageScroll() {
window.scrollBy(0,10); // horizontal and vertical scroll increments
scrolldelay = setTimeout('pageScroll()',100); // scrolls every 100 milliseconds
if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight) {
scrolldelay = setTimeout('PageUp()',2000);
}

}

function PageUp() {
window.scrollTo(0, 0);
}

</script>

</body>
</html>

滚动速度将被改变,这太快了但只是为了测试。该脚本似乎可以完成这项工作,但是当跳转到页面顶部时,在再次向下滚动之前会出现一些“问题”。谁能告诉我哪里出了问题?

最佳答案

试试这个 - 清除第一个 Timeout ,然后在返回顶部后调用 pageScroll 函数。

function pageScroll() {
window.scrollBy(0, 10); // horizontal and vertical scroll increments
scrolldelay = setTimeout('pageScroll()', 100); // scrolls every 100 milliseconds
if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight) {
clearTimeout(scrolldelay);
scrolldelay = setTimeout('PageUp()', 2000);
}

}

function PageUp() {
window.scrollTo(0, 0);
setTimeout(function() {
pageScroll()
}, 600);
}
body {
width: 500px;
}
<body onLoad="pageScroll()">

<h1>Scores</h1>

test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test
test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test
test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br> test test test <br>

关于保持网页滚动的Javascript,慢慢向下滚动,等待,回到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42576759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com