1.效果图
2.代码实现
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./css/index.css">
<title>Document</title>
<style>
.scrollBox{
width: 1400px;
margin: auto;
margin-top: 200px;
overflow: hidden;
}
</style>
</head>
<body>
<div class="scrollBox" id="scrollBox">
<table >
<tr>
<td><img src="./img/01.JPG" alt="" width="350px"></td>
<td><img src="./img/02.JPG" alt="" width="350px"></td>
<td><img src="./img/03.JPG" alt="" width="350px"></td>
<td><img src="./img/04.JPG" alt="" width="350px"></td>
<td><img src="./img/01.JPG" alt="" width="350px"></td>
<td><img src="./img/02.JPG" alt="" width="350px"></td>
<td><img src="./img/03.JPG" alt="" width="350px"></td>
<td><img src="./img/04.JPG" alt="" width="350px"></td>
</tr>
</table>
</div>
<script src="./js/index.js"></script>
</body>
</html>
index.js
let scrollBox = document.getElementById(`scrollBox`);//获得溢出隐藏对应的标签对象
let scrollMove = () => {//声明滚动的方法
if (scrollBox.scrollLeft == 1400) {//判断当前是否移到了最右边
scrollBox.scrollLeft = 0//是的话归0
}
scrollBox.scrollLeft+=1//不是的话继续向右移动
}
let inte = setInterval(scrollMove, 1) //设置间隔setInterval传入方法和对应毫秒数,间隔多少秒执行一次方法,并将结果付给inte
scrollBox.onmouseover = () => { //给最外面的盒子添加鼠标掠过事件
clearInterval(inte) //鼠标掠过时,清除间隔
}
scrollBox.onmouseout = () => {//给最外面的盒子添加鼠标移出事件
inte =setInterval(scrollMove, 1)//鼠标移出,继续执行
}