取element元素的样式值-getComputedStyle方法

来源:互联网 发布:模拟人生4mac怎么放mod 编辑:程序博客网 时间:2024/05/01 04:58

/*
  getComputedStyle方法可以获取到某个element中所有样式属性和属性值
  返回一个CSSStyleDeclaration对象
  通过getPropertyValue(样式属性) 来获取样式值
 
  CSSStyleDeclaration getComputedStyle ( Element elt , String pseudoElt )
 
*/
function _getElementWidth()
{
  var style = document.defaultView.getComputedStyle(element, "");
  var leftMargin = style.getPropertyValue("margin-left");
  leftMargin = leftMargin ? Math.round(parseFloat(leftMargin)) : 0;
  var rightMargin = style.getPropertyValue("margin-right");
  rightMargin = rightMargin ? Math.round(parseFloat(rightMargin)) : 0;
  return element.boxObject.width + leftMargin + rightMargin;
}