js-实现循环播放
效果展示:
1. 点击“开启循环定时器”,启动定时器,图片进行循环播放
2. 点击“清楚循环定时器”,图片循环播放停止
代码实现:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button onclick="start2()">开始循环定时器</button>
<button onclick="stop2()">清除循环定时器</button>
<br>
<div style="border: 1px solid blue;height: 400px" id="div2"/>
<br>
</body>
<script>
let div2 = document.getElementById("div2");
let count=0;
function start2() {
let arr = ["banner_1", "banner_2", "banner_3"]
s2 = setInterval(function () {
if (count == 3) {
count = 0
}
div2.style.backgroundImage = "url(img/"+arr[count]+".jpg)";
count++
}, 1000)
}
function stop2() {
clearInterval(s2)
}
</script>
</html>
很好玩哟,快来试试吧!