getComputedStyle 方法

 

一:getComputedStyle

getComputedStyle是一个能够获取当前元素全部最终使用的CSS属性值。返回的是一个CSS样式声明对象([object CSSStyleDeclaration]),只读。javascript

语法以下:css

var style = window.getComputedStyle("元素", "伪类");

例如:java

var dom = document.getElementById("test"),
    style = window.getComputedStyle(dom , ":after");

注意:Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) 以前,第二个参数“伪类”是必需的(若是不是伪类,设置为null)。如今,不是必需参数了。浏览器

二:getComputedStyle 与 style 的区别

咱们使用element.style也能够获取元素的CSS样式声明对象,可是其与getComputedStyle方法还有有一些差别的。框架

  1. 只读与可写
    正如上面提到的getComputedStyle方法是只读的,只能获取样式,不能设置;而element.style能读能写。
  2. 获取的对象范围
    getComputedStyle方法获取的是最终应用在元素上的全部CSS属性对象(即便没有CSS代码,也会把默认的祖宗八代都显示出来);而element.style只能获取元素style属性中的CSS样式。所以对于一个光秃秃的元素<p>getComputedStyle方法返回对象中length属性值(若是有)就是190+(据我测试FF:192, IE9:195, Chrome:253, 不一样环境结果可能有差别), 而element.style就是0

三:getComputedStyle 与 defaultView

若是咱们查看jQuery源代码,会发现,其css()方法实现不是使用的 window.getComputedStyle 而是 document.defaultView.getComputedStyle,what's all this aboutdom

实际上,使用defaultView基本上是没有必要的,getComputedStyle自己就存在window对象之中。ide

不过有个特殊状况,在FireFox3.6上不使用defaultView方法就搞不定的,就是访问框架(frame)的样式.post

四:getComputedStyle兼容性

getComputedStyle方法IE6~8是不支持的。不过,IE自有本身的一套东西。接下来就介绍的currentStyle就是了     => =>

五:getComputedStyle 与 currentStyle

currentStyle是IE浏览器的一个属性,其与element.style能够说是近亲,至少在使用形式上相似。差异在于element.currentStyle返回的是元素当前应用的最终CSS属性值(包括外链CSS文件,页面中嵌入的<style>属性等)。所以,从做用上将,getComputedStyle方法与currentStyle属性走的很近,形式上则stylecurrentStyle走的近。不过,currentStyle属性貌似不支持伪类样式获取,这是与getComputedStyle方法的差别,也是jQuery css()方法没法体现的一点。测试

getComputedStyle 和 currentStyle 仍是有不少区别的:this

例如,咱们要获取一个元素的高度,能够相似下面的代码:

alert((element.currentStyle? element.currentStyle : window.getComputedStyle(element, null)).height);

结果FireFox下显示24px(通过计算了), 而IE浏览器下则是CSS中的2em属性值。

getComputedStyle 方法与 currentStyle 属性其余具体差别还有不少

6、getPropertyValue方法

getPropertyValue方法能够获取CSS样式申明对象上的属性值(直接属性名称),例如:

window.getComputedStyle(element, null).getPropertyValue("float");

若是咱们不使用getPropertyValue方法,直接使用键值访问,其实也是能够的。可是,好比这里的的float,若是使用键值访问,则不能直接使用getComputedStyle(element, null).float,而应该是cssFloatstyleFloat,天然须要浏览器判断了,比较折腾!

使用getPropertyValue方法没必要能够驼峰书写形式(不支持驼峰写法),例如:style.getPropertyValue("border-top-left-radius");

兼容性
getPropertyValue方法IE9+以及其余现代浏览器都支持。OK,一涉及到兼容性问题(IE6-8肿么办),不急,IE自由一套本身的套路,就是getAttribute方法。

7、getPropertyValue 和 getAttribute

在老的IE浏览器(包括最新的),getAttribute方法提供了与getPropertyValue方法相似的功能,能够访问CSS样式对象的属性。用法与getPropertyValue相似:

style.getAttribute("float");

注意到没,使用getAttribute方法也不须要cssFloatstyleFloat的怪异写法与兼容性处理。不过,仍是有一点差别的,就是属性名须要驼峰写法,以下:

style.getAttribute("backgroundColor");

若是不考虑IE6浏览器,貌似也是能够这么写:

style.getAttribute("background-color");

8、getPropertyValue 和 getPropertyCSSValue

从长相上看getPropertyCSSValuegetPropertyValue是近亲,但实际上,getPropertyCSSValue要顽劣的多。

getPropertyCSSValue方法返回一个CSS最初值(CSSPrimitiveValue)对象(width, height, left, …)或CSS值列表(CSSValueList)对象(backgroundColor, fontSize, …),这取决于style属性值的类型。在某些特别的style属性下,其返回的是自定义对象。该自定义对象继承于CSSValue对象(就是上面所说的getComputedStyle以及currentStyle返回对象)。

getPropertyCSSValue方法兼容性很差,IE9浏览器不支持,Opera浏览器也不支持(实际支持,只是总是抛出异常)。并且,虽然FireFox中,style对象支持getPropertyCSSValue方法,但老是返回null. 所以,目前来说,getPropertyCSSValue方法能够先漠不关心。

相关文章
相关标签/搜索