轮播图自动播放 定时器使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
ul,
li {
margin: 0;
padding: 0;
list-style: none;
}
ul {
width: 500px;
height: 300px;
border: 1px solid black;
position: relative;
}
li {
width: 500px;
height: 300px;
position: absolute;
display: none;
}
img {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<ul id="list">
<li style="display:block"><img src="./images/1.jpg" alt=""></li>
<li><img src="./images/2.jpg" alt=""></li>
<li><img src="./images/3.jpg" alt=""></li>
</ul>
<script>
var liList = document.getElementsByTagName('li')
var index = 0
var intervalId
run()
function run() {
intervalId = setInterval(function () {
index++
for (var i = 0; i < liList.length; i++) {
liList[i].style.display = 'none'
}
index = index % 3
liList[index].style.display = 'block'
}, 1000)
}
list.onmouseover = function () {
clearInterval(intervalId)
}
list.onmouseout = function () {
run()
}
</script>
</body>
</html>