如何在Javascript中管理requestAnimationFrame
JavaScript:如何在Javascript中管理requestAnimationFrame,javascript,if-statement,requestanimationframe,Javascript,If Statement,Requestanimationframe,JavaScript: var chop1 = document.getElementById("chop1"); var hit = document.getElementById("butn"); chop1.style.position = "relative"; chop1.style.top = "0px"; var chopperY; function fly() { chop1.style.top = parseInt(chop1.style.top ) + 10 + 'px
var chop1 = document.getElementById("chop1");
var hit = document.getElementById("butn");
chop1.style.position = "relative";
chop1.style.top = "0px";
var chopperY;
function fly()
{
chop1.style.top = parseInt(chop1.style.top ) + 10 + 'px';
chopperY = chop1.style.top;
if( chopperY == 500)
{
alert("get the position")
}
requestAnimationFrame(fly);
};
hit.onclick = function()
{
fly();
};
在上面的示例中,chopperY==500时没有任何警报。Console没有给出任何错误。此代码中的错误是什么?如何在给定条件下警报该字符串?HTML节点的style.top属性始终是一个包含位置和单位的字符串,在您的情况下,它将是一个“px”,因此,在条件语句中,您应该编写chop1==='500px'{…}