Js/jquery code for obtaining the height, width, and scroll bar height of the visible area of the browser window

Source: Internet
Author: User

Obtain the height and width of the visible area of the browser window. For details about the height of the scroll bar, see.
IE, the browser display window size can only be obtained below: the code is as follows: copy the code
Copy codeThe Code is as follows:
Document. body. offsetWidth
Document. body. offsetHeight

In the declared DOCTYPE browser, you can get the Browser display window size: the code is as follows: copy the code
Copy codeThe Code is as follows:
Document.doc umentElement. clientWidth
Document.doc umentElement. clientHeight

IE, FF, and Safari support this method. Although opera supports this attribute, the page size is returned;
Besides IE, all browsers save this information in the window object. You can use the following command to obtain the information: copy the Code as follows:
Copy codeThe Code is as follows:
Window. innerWidth
Window. innerHeight

The general method for obtaining the entire webpage size is as follows: copy the code
Copy codeThe Code is as follows:
Document. body. scrollWidth
Document. body. scrollHeight

The screen resolution height is generally obtained using the following code: copy the code
Copy codeThe Code is as follows:
Window. screen. height
Window. screen. width

Summary examples
Copy codeThe Code is as follows:
Function getViewSizeWithoutScrollbar () {// does not contain a scroll bar
Return {
Width: document.doc umentElement. clientWidth,
Height: document.doc umentElement. clientHeight
}
}
Function getViewSizeWithScrollbar () {// contains the scroll bar
If (window. innerWidth ){
Return {
Width: window. innerWidth,
Height: window. innerHeight
}
} Else if(document.doc umentElement. offsetWidth = document.doc umentElement. clientWidth ){
Return {
Width: document.doc umentElement. offsetWidth,
Height: document.doc umentElement. offsetHeight
}
} Else {
Return {
Width: document.doc umentElement. clientWidth + getScrollWith (),
Height: document.doc umentElement. clientHeight + getScrollWith ()
}
}
}

The differences between IE and FireFox are as follows:
IE6.0, FF1.06 +:
Copy codeThe Code is as follows:
ClientWidth = width + padding
ClientHeight = height + padding
OffsetWidth = width + padding + border
OffsetHeight = height + padding + border
IE5.0/5.5:
ClientWidth = width-border
ClientHeight = height-border
OffsetWidth = width
OffsetHeight = height

The following is the most common method for retrieving the entire page width (jquery framework is required ).
Copy codeThe Code is as follows:
$ (Document). width () <$ ('body'). width ()? $ (Document). width (): $ ('body'). width ();
$ (Document). height () <$ ('body'). height ()? $ (Document). height (): $ ('body'). height ();

Alert ($ (window). height (); // The height of the visible area of the current window in the browser
Alert ($ (document). height (); // The height of the current window document in the browser
Alert ($ (document. body). height (); // The height of the current window body in the browser
Alert ($ (document. body). outerHeight (true); // the total height of the current window document body in the browser includes border padding margin
Alert ($ (window). width (); // The width of the visible area of the current window in the browser
Alert ($ (document). width (); // The image width of the window document in the browser
Alert ($ (document. body). width (); // The height of the current window body in the browser
Alert ($ (document. body). outerWidth (true); // The total width of the current window body in the browser includes border padding margin

Alert ($ (document). scrollTop (); // gets the vertical height from the scroll bar to the top.
Alert ($ (document). scrollLeft (); // gets the vertical width from the scroll bar to the left.

Related Article

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.