思路
- 首先将li第一项深克隆复制一份到最后一项
- 接着给每一个li定高,每隔一段时间,根据下标移动scrollTop
- 移动scrollTop缓慢过度的效果
- 定义开始的距离,以及结束的距离,两者之间移动多少
- 定义一个总变化时长,单次变化的速度,计算在总时长里一共要变化多少次,每次变化的距离,
- 到达目标距离的时候清空计时器
- 当到达列表的最后一项时,让目标距离变为0, 当前下标变为0

<div class="container">
<h1 class="title">最新公告</h1>
<ul class="list">
<li>1.Lorem ipsum dolor sit.</li>
<li>2.Asperiores provident quidem placeat.</li>
<li>3.Provident aut mollitia tempore.</li>
<li>4.Aspernatur saepe quibusdam fugit?</li>
</ul>
</div>
(function () {
var list = document.querySelector(".list")
function cloneFirstItem() {
var firstItem = list.children[0]
var newItem = firstItem.cloneNode(true)
list.appendChild(newItem)
}
cloneFirstItem()
var duration = 2000
setInterval(moveNext, duration)
var itemHeight = 30
var currentIndex = 0
function moveNext() {
var from = currentIndex * itemHeight
currentIndex++
var to = currentIndex * itemHeight
var totalDuration = 300
var duration = 10
var times = totalDuration / duration
var dis = (to - from) / times
var timerId = setInterval(() => {
from += dis
if (from >= to) {
clearInterval(timerId)
if (currentIndex === list.children.length - 1) {
from = 0
currentIndex = 0
}
}
list.scrollTop = from
}, duration);
}
})()