Jquery code for obtaining the width and height of the browser screen

Source: Internet
Author: User

This article describes how to use jquery to obtain the width and height of the browser screen. For more information, see.

Obtain the screen size

In the beginning, I encountered a problem. I want to create a pop-up div layer first, and then set the z-index attribute and the height and width background color,
I use $ (document ). height (), $ (document ). width () to get the window height and width, but double-click the browser to change the window size, this height does not change...

The Code is as follows: Copy code

CSS code

<Style type = "text/css">
# Cover {
Width: 100%;
Background-color: # CCCCCC;
Z-index: 99;
Display: none;
}
</Style>


HTML code

<Body>
<Input type = "button" id = "btn" value = "show"/>
<Div id = "cover"> </div>
</Body>


JScript code


Var btn = $ ("# btn ");
Var show = $ ("# cover ");
Btn. click (function (){
Show.css ({"position": "absolute", "top": "0", "left": "0 "});
Show. height ($ (document). height ());
Show. show ();
});
</Script>


 

Later, Baidu found some code about jquery to get the width and height of the browser screen.

The Code is as follows: Copy code


$ (Document). ready (function (){
Alert ($ (window). height (); // The visible area height 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 file body in the browser window
Alert ($ (document. body). outerHeight (true); // the total height of the current file body in the browser window includes border padding margin.
Alert ($ (window). width (); // The width of the visible area of the current window in the browser
Alert ($ (document). width (); // width of the document Object in the current window of the browser
Alert ($ (document. body). width (); // The width of the document body in the current window of the browser
Alert ($ (document. body). outerWidth (true); // The total width of the current file body in the browser window includes border padding margin
})


 

Returns the height and width of the current screen.

The Code is as follows: Copy code


<Script type = "text/javascript">
$ (Document). ready (function ()
{
Alert ($ (window). height (); // The visible area height 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 file body in the browser window
Alert ($ (document. body). outerHeight (true); // the total height of the current file body in the browser window includes border padding margin.
Alert ($ (window). width (); // The width of the visible area of the current window in the browser
Alert ($ (document). width (); // width of the document Object in the current window of the browser
Alert ($ (document. body). width (); // The height of the current file body in the browser window
Alert ($ (document. body). outerWidth (true); // The total width of the current file body in the browser window includes border padding margin
}
)
</Script>


Browser compatibility, so I found some summary on the Internet.

The Code is as follows: Copy code

Alert ($ (window). height (); // The visible area height 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 file body in the browser window

Alert ($ (document. body). outerHeight (true); // the total height of the current file body in the browser window includes border padding margin.

Alert ($ (window). width (); // The width of the visible area of the current window in the browser

Alert ($ (document). width (); // width of the document Object in the current window of the browser

Alert ($ (document. body). width (); // The height of the current file body in the browser window

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

 

// Obtain the page height and width

Function getPageSize (){

Var xScroll, yScroll;

If (window. innerHeight & window. scrollMaxY ){

XScroll = window. innerWidth + window. scrollMaxX;

YScroll = window. innerHeight + window. scrollMaxY;

} Else {

If (document. body. scrollHeight> document. body. offsetHeight) {// all but Explorer Mac

XScroll = document. body. scrollWidth;

YScroll = document. body. scrollHeight;

} Else {// Explorer Mac... wocould also work in Explorer 6 Strict, Mozilla and Safari

XScroll = document. body. offsetWidth;

YScroll = document. body. offsetHeight;

}

}

Var implements wwidth, implements wheight;

If (self. innerHeight) {// all privileges t Explorer

If (document.doc umentElement. clientWidth ){

Required wwidth = document.doc umentElement. clientWidth;

} Else {

Required wwidth = self. innerWidth;

}

Optional wheight = self. innerHeight;

} Else {

If (document.doc umentElement & document.doc umentElement. clientHeight) {// Explorer 6 Strict Mode

Required wwidth = document.doc umentElement. clientWidth;

Required wheight = document.doc umentElement. clientHeight;

} Else {

If (document. body) {// other Explorers

Required wwidth = document. body. clientWidth;

Optional wheight = document. body. clientHeight;

}

}

}

// For small pages with total height less then height of the viewport

If (yScroll <windowHeight ){

PageHeight = running wheight;

} Else {

PageHeight = yScroll;

}

// For small pages with total width less then width of the viewport

If (xScroll <1_wwidth ){

PageWidth = xScroll;

} Else {

PageWidth = required wwidth;

}

ArrayPageSize = new Array (pageWidth, pageHeight, expires wwidth, expires wheight );

Return arrayPageSize;

}

 

// Scroll bar

Document. body. scrollTop;

$ (Document). scrollTop ();

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.