How to use javascript to obtain the height and width of a hidden element (display: none) _ javascript tips

Source: Internet
Author: User
This article describes how to use javascript to obtain the height and width of a hidden element (display: none, if you need a friend, you can refer to js to obtain the size of visible elements. This method can be used directly:

The Code is as follows:


Function getDefaultStyle (obj, attribute) {// return the final style function, compatible with IE and DOM, and set parameters: Element Object and Style Features
Return obj. currentStyle? Obj. currentStyle [attribute]: document. defaultView. getComputedStyle (obj, false) [attribute];
}



However, if this element is hidden (display: none) and the size is unadaptive, the above method won't work! Because the display: none element has no physical size! This is the tragedy!

Fortunately, there is visibility: hidden in css, which is invisible. The biggest difference between it and display: none is that visibility: hidden has physical size. The physical size can be obtained through the above method, but after the display: none is changed to visibility: hidden, the page will be blank, even if you change visibility: hidden to display: none immediately after obtaining the size, the page will still shake. The best way is to remove the hidden element from the screen or exit the Document Stream (position: absolute ). This seems to be perfect, but the tragedy has happened again. If you want to display this element again, this element is invisible and its location is not correct, because this element is visibility: hidden; position: absolute. Therefore, you must restore the style after obtaining the size. The position and visibility attributes are set back to the original style.

This is the basic implementation method for js to obtain the size of hidden elements. If you are interested, please refer to the method in the book "proficient in javascript.

I also made a simple demo here. You can look at the source code:

The Code is as follows:





Js to obtain the size of hidden elements

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.