brief description of the problem: this code is long, it is recommended to copy it to the editor. The problem is to use two js methods to achieve an effect. I personally think that the principle of the two methods is the same, but one effect has come out, and one has no effect and reported an error. If you want to know the reason for reporting an error, hope God for advice.
HTML Code:
requestAnimationFrame seTtimeout :
<div>
<p class="ball red" style="margin-left:0px;">
<p class="ball green" style="margin-left:0px;">
<p class="ball blue" style="margin-left:0px;">
</div>
CSS Code:
.ball {
width: 20px;
height: 20px;
border-radius: 50%;
}
.red {
background-color: red;
}
.green {
background-color: green;
}
.blue {
background-color: blue;
}
JavaScript Code method 1:
var red = document.querySelector(".red");
var green = document.querySelector(".green");
var blue = document.querySelector(".blue");
ani(red, 100, function() {
ani(green, 200, function() {
ani(blue, 150, function() {
ani(red, 150);
ani(green, 150)
})
})
})
//
function ani(node, to, callback) {
animate();
function animate() {
var marginLeft = parseInt(node.style.marginLeft);
if (marginLeft == to) {
callback && callback();
} else {
if (marginLeft < to) {
marginLeftPP
} else if (marginLeft > to) {
marginLeft--
}
node.style.marginLeft = marginLeft + "px";
requestAnimationFrame(animate);
}
}
}
effect is fine:
JavaScript:
:
question: what is the difference between the two scenarios? Why is the second option wrong?