gpt4 book ai didi

javascript - 如何使列表从下到上动画化

转载 作者:行者123 更新时间:2023-11-30 17:11:44 27 4
gpt4 key购买 nike

我正在创建一个小动画,它的工作方式类似于从上到下,一旦它滚动到最后,它就会从下到上,并且会一次又一次地重复。

<style>
#scrollList{height:150px; width:200px; overflow:auto; margin:40px auto;}
#scrollList li{height:45px;}
</style>
<script type="text/javascript">
//document.getElementById("scrollList").setAttribute("onClick","scrollList()");
function scrollList() {
//var scrollHeight = document.getElementById("scrollList").scrollTop;
//console.log(scrollHeight);

var ActualscrollHeight = document.getElementById("scrollList").clientHeight;
console.log(ActualscrollHeight);

setInterval(function(){
var scrollHeight = document.getElementById("scrollList").scrollTop;
if(scrollHeight <= ActualscrollHeight){
scrollHeight = scrollHeight + 1;
//console.log(scrollHeight);
document.getElementById("scrollList").scrollTop=scrollHeight;
}


}, 3);
}
</script>

<body onload="scrollList()">
<ul id="scrollList">
<li>Scroll Text</li>
<li>Scroll Text</li>
<li>Scroll Text</li>
<li>Scroll Text</li>
<li>Scroll Text</li>
<li>Scroll Text</li>
<li>Scroll Text</li>
</ul>

最佳答案

试试这个:

function scrollList() {
var ActualscrollHeight = document.getElementById("scrollList").clientHeight;
var down = true;
setInterval(function () {
var scrollHeight = document.getElementById("scrollList").scrollTop;
if (scrollHeight == 0) {
down = true;
} else if (scrollHeight >= ActualscrollHeight) {
down = false;
}
scrollHeight = (!down) ? scrollHeight - 1 : scrollHeight + 1;
document.getElementById("scrollList").scrollTop = scrollHeight;
}, 3);
}

Fiddle

关于javascript - 如何使列表从下到上动画化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26858914/

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