获取样式
作者:互联网
<!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>
标签:function,oDiv,attr,width,样式,获取,obj,currentStyle 来源: https://www.cnblogs.com/Fourteen-Y/p/11957783.html