zoukankan      html  css  js  c++  java
  • 封装getStyle (获取样式currentStyle getComputedStyle兼容处理)

    <script type="text/javascript">
    //哪个元素
    //哪个样式

    function getStyle(obj, attr)
    {
     if(obj.currentStyle)
     {
      return obj.currentStyle[attr];
     }
     else
     {
      return getComputedStyle(obj, false)[attr];
     }
    }

    window.onload=function ()
    {
     var oDiv=document.getElementById('div1');
     
     alert(getStyle(oDiv, 'backgroundColor'));
    }
    </script>

     

    <div id="div1"></div>

    //获取样式简洁版
    function getStyle(obj, attr) {
    	return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj, false)[attr];
    }
    //opacity 设置透明度
    function setOpacity(elem, value) {
    	elem.filters ? elem.style.filter = 'alpha(opacity=' + value + ')' : elem.style.opacity = value / 100;
    }


    //完美版
    function css(obj, attr, value){
    	switch (arguments.length){
    		case 2:
    			if(typeof arguments[1] == "object"){
    				for (var i in attr) i == "opacity" ? (obj.style["filter"] = "alpha(opacity=" + attr[i] + ")", obj.style[i] = attr[i] / 100) : obj.style[i] = attr[i];
    			}else{
    				return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj, null)[attr]
    			}
    			break;
    		case 3:
    			attr == "opacity" ? (obj.style["filter"] = "alpha(opacity=" + value + ")", obj.style[attr] = value / 100) : obj.style[attr] = value;
    			break;
    	}
    };
  • 相关阅读:
    ....
    排序相关的问题(jq,java)_1123
    Spring aop 记录操作日志
    vue -element ui 自定义验证规则,封装在公共的文件里
    vue
    ES6 新特性
    正则表达式
    面向对象基础--继承(2)
    面向对象基础(1)
    安装vue环境
  • 原文地址:https://www.cnblogs.com/leejersey/p/2642604.html
Copyright © 2011-2022 走看看