Currentstyle and getComputedStyle are compatible. getcomputedstyle

Source: Internet
Author: User

Currentstyle and getComputedStyle are compatible. getcomputedstyle
CurrentStyle: Get the calculated style, also called the current style and final style.

Advantage: You can get the final style of the element, including the default value of the browser. Unlike style, you can only get the line style, so it is more commonly used.
Note: you cannot obtain attribute values of compound styles, such as background. You can only obtain a single style, such as background-color.

Alert (oAbc. currentStyle );
Unfortunately, this amazing thing cannot be perfectly supported by various browsers. To be precise, in the browser I tested, "object CSSStyleDeclaration" is displayed in IE8 and Opera 11; and "undefined" is displayed in FF 12, chrome 14, and safari 5 ".

Although currentStyle cannot be applied to all browsers, you can separate the supported and unsupported browsers Based on the test results, and then find the compatible method.

Var oAbc = document. getElementById ("abc ");
If (oAbc. currentStyle ){
// IE, Opera
Alert ("I support currentStyle ");
} Else {
// FF, chrome, safari
Alert ("I do not support currentStyle ");
}
In the FF browser, we can use getComputedStyle (obj, false) to achieve the same effect as currentStyle in IE.

GetComputedStyle (obj, false): In the new FF version, you only need the first parameter, that is, the operation object. The second parameter, "false", is also a common method, the purpose is to be compatible with Firefox browsers of earlier versions.

Compatible Syntax:

Var oAbc = document. getElementById ("abc ");
If (oAbc. currentStyle ){
// IE, Opera
// Alert ("I support currentStyle ");
Alert (oAbc. currentStyle. width );
} Else {
// FF, chrome, safari
// Alert ("I do not support currentStyle ");
Alert (getComputedStyle (oAbc, false). width );
}
The id = "abc" of the body in a blank page. Test the above Code. "auto" is displayed for IE and Opera, and "*** px" is displayed for the other three browsers ". Although the results are different, it can be found that chrome and safari are the same as Firefox, and the attribute values are successfully read. The three browsers (FF, chrome, and safari) that do not support currentStyle support getComputedStyle.

The results show that the browser supports currentStyle + getComputedStyle to ensure compatibility with mainstream browsers. And compatible writing is not complex. Do you have mastered it? Pai_^

Support currentStyle: IE, Opera
Supports getComputedStyle: FireFox, Chrome, Safari

Pay attention to the final pop-up content. currentStyle returns the default browser value "auto", while getComputedStyle returns the specific value "** px ".
It is NAN problem for javascript to obtain the returned value of the getComputedStyle () style.

Function getStyle (obj, attr ){
If (obj. currentStyle ){
Return obj. currentStyle [attr]
}
Else {
Return getComputedStyle (obj, false) [attr]
}
}
Okay, I got an error. I thought you wanted to get compatible styles. This function is used to obtain the object property value. If NAN is returned, the object property is not a numeric value.

JS offsetTop compatibility

On the homepage, native js does not have the getComputedStyle function, which may be a js framework computing method,
The following functions can be used to obtain

Function getOffsetTop (elm ){
Var mOffsetTop = elm. offsetTop;
Var mOffsetParent = elm. offsetParent;
While (mOffsetParent ){
MOffsetTop + = mOffsetParent. offsetTop;
MOffsetParent = mOffsetParent. offsetParent;
}
Return mOffsetTop;
}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.