获取样式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>获取样式</title>
    <style>
        #div1{
            width:200px;
            height:200px;
            background: #f00;
            border:4px solid #000;
            font-size:12px;
            color: #fff;;
        }
    </style>
    <script>
        window.onload=function(){
            startMove();
        };
        function startMove(){
          setInterval(function(){
              var oDiv=document.getElementById('div1');
              oDiv.style.width = parseInt(getStyle(oDiv,'width')) - 1 + 'px';
              oDiv.style.fontSize = parseInt(getStyle(oDiv,'fontSize')) + 1 + 'px';
          },30)
        }
        function getStyle(obj,attr){         //封装函数,获取样式专用
            if(obj.currentStyle){
                return obj.currentStyle[attr];   //currentStyle针对IE浏览器
            }else{
                return getComputedStyle(obj,false)[attr];  //getComputedStyle针对firefox专用
            }
        }
    </script>
</head>
<body>
    <div id="div1">啦啦啦啦</div>
</body>
</html>

 

 

posted @ 2019-11-29 14:58  Fourteen  阅读(189)  评论(0编辑  收藏  举报