想做一个loading动画,三个小球依次上下跳动
不造哪里出了问题,一个小球调用时,速度看起来还算正常,三个调用速度六到飞起。。。【尴尬脸】
问:如何能让小球依次跳动??
相关代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
#wrap {
margin: 0 auto;
margin-top: 45%;
width: 100px;
height: 50px;
border: 1px solid palegreen;
position: relative;
}
span {
position: absolute;
display: inline-block;
width: 20px;
height: 20px;
border-radius: 50%;
background: palevioletred;
}
#ball1 {
left: 0px;
}
#ball2 {
left: 25px;
/* margin-top: 5px; */
}
#ball3 {
left: 50px;
/* margin-top: 10px; */
}
</style>
</head>
<body>
<div id="wrap">
<span id="ball1"></span>
<span id="ball2"></span>
<span id="ball3"></span>
</div>
<script>
var wrapTop = document.getElementById('wrap');
var ball1Top = document.getElementById('ball1');
var ball2Top = document.getElementById('ball2');
var ball3Top = document.getElementById('ball3');
var i = 0,
j, ball1, ball2, ball3;
function loadingBall(ele) {
// cancelAnimationFrame(t);
var t = ele.getAttribute("id");
function up() {
i++;
if (i < 30) {
t = window.requestAnimationFrame(up);
} else {
j = i;
t = window.requestAnimationFrame(down);
}
ele.style.top = i +
'px'; // ball2Top.style.top=i + 'px' ; // ball3Top.style.top=i + 'px' ; }
}
function down() {
j--;
if (j > 0) {
t = window.requestAnimationFrame(down);
} else {
i = 0;
t = window.requestAnimationFrame(up);
}
ele.style.top = j + 'px';
// ball2Top.style.top = j + 'px';
// ball3Top.style.top = j + 'px';
}
t = window.requestAnimationFrame(up);
console.log(t);
}
loadingBall(ball1Top);
//此处注释,看一个小球速度,再把这两句加上看速度
// loadingBall(ball2Top);
// loadingBall(ball3Top);
</script>
</body>
</html>
计数的变量有冲突,互相影响了,放到函数里面就好了