哪里错啦?老是报ReferenceError: attr is not defined这个,我上次错了别的语法也这么报,估计是其他原因写了attr没问题

来源:6-2 完美运动框架

慕仙5237505

2017-11-01 21:46

<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)
        }

写回答 关注

1回答

  • qq_Moderato_0
    2017-11-02 16:30:12
    已采纳

    没有attr呀~ 你代码里的 应该把attr改为i,for循环里 json的属性名。

    慕仙5237...

    非常感谢!啊,终于对了找了好久,谢谢

    2017-11-03 11:12:58

    共 1 条回复 >

JS动画效果

通过本课程JS动画的学习,从简单动画开始,逐步深入各种动画框架封装

113923 学习 · 1443 问题

查看课程

相似问题