Home > Article > Web Front-end > A handwritten javascript getStyle function compatible with various browsers (getting the style of an element)_javascript skills
There have always been many compatibility issues in getting the calculated style of HTML elements. There are some differences in each browser. Firefox and webkit (Chrome, Safari) support the W3C standard method: getComputedStyle(), while IE6/7/ 8 does not support the standard method but has private properties to implement it: currentStyle, both IE9 and Opera support it. With these two methods and attributes, most requirements can basically be met.
But for adaptive width and height, you cannot get the calculated value using currentStyle. You can only return auto, and getComputedStyle() can return the calculated value. There are several ways to solve this problem. What I thought of before was to subtract the padding value from clientWidth/clientHeight, so that the calculated width and height can be obtained in browsers that do not support the standard method. A few days ago, I saw that Situ Zhengmei used another method, using the getBoundingClientRect() method to get the position of the element on the page, and then subtracting right from left is the width, and bottom minus top is the height. I made some small modifications to his code, and the final code is as follows:
var val = elem.currentStyle[style];
if( val === 'auto' && (style === "width" || style === "height") ){
var rect = elem.getBoundingClientRect();
if( style === "width" ){
return rect.right - rect.left 'px';
}else{
return rect.bottom - rect.top 'px';
}
}
return val;
}();
};
// Call this method
var test = document.getElementById( 'test' ),
// Get the calculated width
tWidth = getStyle( test, 'width' );
New question, if the width or height of the element uses em or % units, the value returned by getComputedStyle() will automatically change em or % into px units, but currentStyle will not, and if it is font- size uses em as the unit, and returns 0em under Opera. Opera is really scary!
Later, I found some unexpected compatibility issues during use. Today I optimized the original code and dealt with some common compatibility issues.
In JavaScript, "-" (dash or hyphen) represents a minus sign, and in CSS, many style attributes have this symbol, such as padding-left, font-size, etc., so If the following code appears in javascript, it is an error:
For float, it is a reserved word in JavaScript. There are other alternative ways to set or get the float value of an element in JavaScript. In standard browsers, it is cssFloat, and in IE6/7/8, it is styleFloat. .
If top, right, bottom, and left do not have an explicit value, some browsers will return an auto when obtaining these values. Although the auto value is a legal CSS attribute value, it is by no means what we want. The result should be 0px.
To set the transparency of elements in IE6/7/8, you need to use filters, such as: filter:alpha(opacity=60). For standard browsers, just set opacity directly. IE9 supports both writing methods. I Compatibility has also been implemented for obtaining the transparency of elements. As long as you use opacity, you can obtain the transparency values of all browser elements.
Getting the width and height of elements in IE6/7/8 has been introduced in the previous article, so I won’t repeat it here. Another thing to note is that if the style of the element is written using style inline, or the style attribute has been set using JavaScript, you can use the following method to get the calculated style of the element:
This method is faster than reading the attribute value in getComputedStyle or currentStyle, and should be used first. Of course, the prerequisite is that the style is set by inline writing (using javascript to set the inline style is also set). The optimized final code is as follows:
The following is an example call: