在运行《javascript王者归来》的一个圆形轨迹例子,发现他在IE中能正常运行,但在firefox中却一直不动,js代码如下:
var r=50; var ins=1; var round = document.getElementById("round"); function Circle() { x=r * Math.cos(ins) + 100; y=r* Math.sin(ins) + 100; ins += 0.02; round.style.left = x; round.style.top = y; } setInterval(Circle,1);
这段代码在IE下能正常运行,但在firefox下就不动,在firebug上看到round的style属性没变化,想必应该是
round.style.left = x; round.style.top = y;
这两句没起作用,经网上一查,发现在firefox上动态定位需要叫上"px"才有效果,即改成
round.style.left = x + "px"; round.style.top = y + "px";
就能在两个浏览器上正常运行了。