获取任意一个元素的任意一个属性的当前值

获取任意一个元素的任意一个属性的当前的值---当前属性的位置值

function getStyle(element,attr){

    return  window.getComputedStyle?window.getComputedStyle(element,null)[attr]:element.currentStyle[attr]||0;

};

动画

function animate(element,attr,target){
    clearInterval(element.timeId);
    element.timeId=setInterval(function(){
    var current=getStyle(element,attr);
    var step=(target-current)/10;
    step = step > 0 ? Math.ceil (step) : Math.floor(step);
    current+=step;
    element.style[attr]=current+"px";
    if(current==target){
        clearInterval(element.timeId);
}
     //测试代码
    console.log("目标"+target+",当前:"+current+",每次移动的步数"+step);
    },20);
};

 

猜你喜欢

转载自blog.csdn.net/weixin_41829477/article/details/81218052
今日推荐