<!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>动画过渡</title>
<script src="jquery-3.4.1.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 100%;
display: flex;
}
.box_child {
width: 200px;
height: 100px;
margin: 20px;
background-color: red;
}
</style>
</head>
<body>
<div class="box">
<div class="box_child"></div>
<div class="box_child"></div>
<div class="box_child"></div>
<div class="box_child"></div>
<div class="box_child"></div>
</div>
<script>
let times = setInterval(fun, 1000); // 设置永久定时器
let index = 0 // 当前过渡到的元素数量
// 获取父元素
let box = document.getElementsByClassName('box')[0];
let box_child = document.getElementsByClassName('box_child');
function fun() {
// 如果动画执行完毕就重新开始
if (index == 5) {
index = 0;
} else {
// 否则动画就继续执行
index++;
}
for (let i = 0; i < box_child.length; i++) {
if (index > i) {
box_child[i].style.backgroundColor = 'blue'
} else {
box_child[i].style.backgroundColor = ''
}
}
}
</script>
</body>
</html>