运动框架

一、获取非行间样式
类似div.style.height只能获取行内样式
div.offsetHeight当前高度,包含内外边距及边框
获取非行间样式:

function getAttr(obj, attr) {
    var style;
    if(window.getComputedStyle) {
        style = getComputedStyle(obj, false)[attr]; //主流浏览器
    } else {
        style = obj.currentStyle[attr]; //兼容IE
    }
    return style;
}

1.window.getComputedStyle(obj,false)[‘attr’]

这是BOM(浏览器window对象)提供的方法 ,所以可以直接写成getComputedStyle(nodeObj,false),省略前面的window对象,该方法有两个参数,第一个是要获取样式的节点对象;第二个可以写成任何的字符一般写成false或者null(兼容老版火狐),这里最好是用false因为用null IE9+会有问题;后面直接跟要获取的样式(写在方括号中)即可。这个方法的返回值为一个对象,为计算后的样式的属性值对的集合。比如要获取某个div宽度。那么可以直接写成 var style=getComputedStyle(div,false)[‘width’]; alert(style);

2.nodeObj.currentStyle[‘attr’];

node.currentStyle,该属性返回的也是是一个对象,也是计算后的样式的属性值对的集合。比如要获取某个div宽度。那么可以直接写成

var style=div.currentStyle['width']; 
alert(style);

二、运动框架 startMove函数

<script>
function startMove(obj,json,fn){
    clearInterval(obj.timer);
    var flag=true;
    obj.timer=setInterval(function(){
        for(var attr in json){
            var icur=0;
            if(attr=='opacity'){
                icur=Math.round(parseFloat(getStyle(obj,attr))*100);
            }else{
                icur=parseInt(getStyle(obj,attr));
            }
            /*匀速
            var speed=json[attr]>icur?10:-10;
            */
            //变速
            var speed=(json[attr]-icur)/5;
            speed=speed>0?Math.ceil(speed):Math.floor(speed);
            if(icur!==json[attr]){
                flag=false;
            }else{
                flag=true;
            }
            if(attr=='opacity'){
                obj.style.filter='(opacity:'+(icur+speed)+')';
                obj.style.opacity=(icur+speed)/100;
            }else{
                obj.style[attr]=icur+speed+'px';
                console.log('1');
            }
        }
        if(flag){
            clearInterval(obj.timer);
            if(fn){
                fn();
            }
        }
    },30);
}
function getStyle(obj,attr){
    var style;
    if(window.getComputedStyle){
        style=getComputedStyle(obj,false)[attr];
    }else{
        style=obj.currentStyle[attr];
    }
    return style;
}
</script>

1.链式运动

<script>
window.onload=function(){
    var oDiv1=document.getElementById("div1");
    oDiv1.onmouseover=function(){
        startMove(oDiv1,{"width":100},function(){
            startMove(oDiv1,{"height":100},function(){
                startMove(oDiv1,{"opacity":100});
            });
        });
    };
}
</script>

2.同时运动

<script>
window.onload=function(){
    var oDiv1=document.getElementById("div1");
    oDiv1.onmouseover=function(){
        startMove(oDiv1,{"width":100,"height":100,"opacity":100});
    };
}
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值