为什么starMove()没有触发

来源:4-3 任意属性值(一)

yanniduoduo

2015-11-20 17:36

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>多物体动画+获取样式2</title>
        <style>
            ul,li{list-style: none;}
            ul li{width: 200px;height: 100px;background-color: yellow;margin-bottom: 20px;
                  border:10px solid #0000FF;}
        </style>
        <script>
            window.onload=function(){
            var Li1= document.getElementById('li1');
            var Li2= document.getElementById('li2');
            Li1.onmouseover=function(){
                startMove(this,'height',400);
            }
            Li1.onmouseout=function(){
                startMove(this,'height',100);
            }
            Li2.onmouseover=function(){
                startMove(this,'width',400);
            }
            Li2.onmouseout=function(){
                startMove(this,'width',200);
            }   
          }
            function getStyle(obj,attr){  /*此函数用来获取样式*///可以改变宽高、背景属性、字号属性、边框属性等等
               if(obj.currentStyle){
                   return obj.currentStyle[attr];/*currentStyle针对IE*/
               }
               else{
                   return getComputedStyle(obj,false)[attr];/*getComputedStyle针对Firefox*/
               }
           }
//            var timer=null;
            function startMove(obj,attr,iTarget){
                clearInterval(obj.timer);
                obj.timer=setInterval(function(){
                    var icur = parseInt(getStyle(obj,attr));
                    var speed=(iTarget-icur)/8;
                    speed = speed>0?Math.ceil(speed):Math.floor(speed);
                    if(icur==iTarget){
                        clearInterval(obj.timer);
                    }
                    else{
                        obj.style.[attr]=icur+speed+'px';
                    }
                },30)
            }
        </script>
    </head>
    <body>
        <ul>
            <li id='li1'></li>
            <li id='li2'> </li>

        </ul>
    </body>
</html>


写回答 关注

2回答

  • Christal1994
    2016-05-15 22:04:52

    我也有一样的问题,没有触发startMove函数,不是“.”的问题,亲,要是解决了,可以告诉我一下吗

  • 李晓健
    2015-11-20 20:22:13
    obj.style[attr]=icur+speed+'px';

    48行多了一个 . 

JS动画效果

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

113925 学习 · 1443 问题

查看课程

相似问题