哪里错啦?老是报ReferenceError: attr is not defined这个,我上次错了别的语法也这么报,估计是其他原因写了attr没问题
<script src="test.js"></script>
<script type="text/javascript">
window.onload=function(){
var li=document.getElementById("li1");
li.onmousemove=function(){
move(li,{width:300,height:300});
}
}
//var json={a:1,b:2};
</script>
function getstyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
}
var alpha=30;
function move(obj,json,fn){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
for(var i in json){
var curr=0;
if(attr=="opacity"){
curr = Math.round(parseFloat(getstyle(obj,attr))*100);
}else{
curr=parseInt(getstyle(obj,attr));
}
//算速度
var speed=(json[attr]-curr)/8;
speed=speed>0?Math.ceil(speed):Math.floor(speed);
//检测停止
if(curr==json[attr]){
clearInterval(obj.timer);
if(fn){
fn();
}
}else{
if(attr=="opacity"){
obj.style.filter="alpha(opacity:"+(curr+speed)+")";
obj.style.opacity=(curr+speed)/100;
}else{
obj.style[attr]=curr+speed+"px";
}
}
}
},30)
}