Js实现无缝连续滚动特效
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
#box{
width: 850px;
border: 4px solid #000;
margin: 50px auto;
overflow: hidden;
}
#box #imglist{
position: relative;
display: flex;
width: 2000px;
}
#box ul{
list-style: none;
}
</style>
</head>
<body>
<div id="box">
<ul id="imgList">
<li>
<img src="https://s1.ax1x.com/2020/10/11/0g5pVO.th.png" alt="" />
</li>
<li>
<img src="https://s1.ax1x.com/2020/10/11/0g5FGd.th.png" alt="" />
</li>
<li>
<img src="https://s1.ax1x.com/2020/10/11/0g5udS.th.png" alt="" />
</li>
<li>
<img src="https://s1.ax1x.com/2020/10/11/0g5YLV.th.png" alt="" />
</li>
<li>
<img src="https://s1.ax1x.com/2020/10/11/0g5aoF.th.png" alt="" />
</li>
<li>
<img src="https://s1.ax1x.com/2020/10/11/0g50JJ.th.png" alt="" />
</li>
</ul>
</div>
<script>
let oBox = document.getElementById("box");
let imgList = document.getElementById("imgList");
//复制多一遍所有的li
imgList.innerHTML+=imgList.innerHTML;
//设置定时器
let timer;
//设置ul的水平移动距离
let left = 0;
move()
function move(){
//设表先关
clearInterval(timer);
timer=setInterval(function (){
left-=4;
if(left<-960){
left=0;
}
imgList.style.transform = `translateX(${left}px)`
},20);
}
//鼠标进入时暂停定时器
oBox.onmouseenters = function (){
clearInterval(timer)
};
//鼠标移开后继续
oBox.onmouseleave = function (){
move()
}
</script>
</body>
</html>