定时器使用实现的动画,宽度、位置等属性

所涉及到的知识点是:定时的作用以及如何获取元素对象css属性的值。

function getAction(obj,attr,value){
      var object=getObj(obj);
      var attrValue=parseInt(getAttribute(object,attr));
      var speed=attrValue>value?-10:10;
      timer=setInterval(function(){
          if((speed>0&&attrValue>=value)||(speed<0&&attrValue<=value)){
            clearInterval(timer);
        }
          else{
              object.style[attr]=attrValue+speed+'px';
            attrValue=parseInt(getAttribute(object,attr));
          }
      },30);
}
function getObj(object){
    return document.getElementById(object)?document.getElementById(object):document.getElementsByTagName(object);
}
function getAttribute(obj,attr){
    return window.getComputedStyle ? window.getComputedStyle(obj,'')[attr] : obj.currentStyle[attr];
}

通过getObj()这个函数获得需要操作的对象,在通过getAttribute()这个函数获得该对象对应的属性的值,并使用getAction()这个函数实现动画效果。简介易懂。

posted on 2017-07-21 10:51  Bean_love  阅读(230)  评论(0编辑  收藏  举报

导航