var temp = 0;
// var divWidth = document.getElementById("moveDiv").style.width;
function move(){
var divWidth = document.getElementById("moveDiv").style.width.split("p")[0];
if( temp < divWidth )
temp += 2;
// alert(divWidth+"ddd"+temp);
document.getElementById("moveDiv").style.marginLeft = temp +"px";
//alert(document.getElementById("moveDiv").style.marginLeft);
setTimeout( move(),1000);
}这段代码有什么问题吗?每次执行move()事件moveDiv就瞬间移到目标位置了,没有每秒移到几个像素的过渡效果。
这是为什么呢?
var temp = 0;
function move(){
var divWidth = parseInt(document.getElementById("moveDiv").style.width);
if( temp < divWidth ) temp += 2;
document.getElementById("moveDiv").style.marginLeft = temp +"px";
setTimeout("move()",100);
}
window.onload=function(){
move();
}
</script>
<div id="moveDiv" style="width:400px;height:120px;background-Color:#009900;"></div>