JS get browser window size Get screen, browser, web page height and width

The width of the visible area of ​​the
web page: document.body.clientWidth The height
of the visible area of ​​the
web page: document.body.clientHeight Width)
Web page body full text width: document.body.scrollWidth
Web page text full text height: document.body.scrollHeight
Web page scrolled height: document.body.scrollTop
web page scrolled left: document.body.scrollLeft
on the web page body part : window.screenTop
page body part left: window.screenLeft
height of
screen resolution: window.screen.height width of screen resolution: window.screen.width
screen available workspace height: window.screen.availHeight
screen available workspace width : window.screen.availWidth


HTML precise positioning: scrollLeft, scrollWidth, clientWidth, offsetWidth
scrollHeight: Get the scroll height of the object.
scrollLeft: Set or get the distance between the left edge of the object and the leftmost of the currently visible content in the window
scrollTop: Set or get the distance between the top of the object and the top of the visible content in the window
scrollWidth: Get the scroll width of the object
offsetHeight: Get the height of the object relative to the layout or the parent coordinate specified by the parent coordinate offsetParent property
offsetLeft: Get the calculated left position of the object relative to the layout or the parent coordinate specified by the offsetParent property
offsetTop: Get the object relative to the layout or by the offsetTop property The calculated top position of the specified parent coordinate
event.clientX The horizontal coordinate
relative to the document event.clientY The vertical coordinate relative to the document
event.offsetX The horizontal coordinate
relative to the container event.offsetY The vertical coordinate relative to the container
document.documentElement.scrollTop The vertical scrolling The value of
event.clientX+document.documentElement.scrollTop is relative to the horizontal coordinate of the document + the amount of scrolling in the vertical direction.

IE and FireFox are different as follows:

IE6.0, FF1.06+:
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

(Need to be mentioned: the margin property in CSS, and clientWidth, offsetWidth, clientHeight, offsetHeight are irrelevant)
The width of the visible area of ​​the
web page: document.body.clientWidth The height
of the visible area of ​​the
web page: document.body.clientHeight Height)
Web page body full text width: document.body.scrollWidth
Web page text full text height: document.body.scrollHeight
Web page scrolled height: document.body.scrollTop
web page scrolled left: document.body.scrollLeft
web page body part : window.screenTop
page body part left: window.screenLeft
screen resolution height: window.screen.height
screen resolution width: window.screen.width
screen available workspace height: window.screen.availHeight
screen available workspace width : window.screen.availWidth
-------------------
Technical points
The code in this section mainly uses some properties of the Document object about the window. The main functions and usage of these properties are as follows.
To get the size of the window, different properties and methods need to be used for different browsers: to detect the real size of the window, you need to use the properties of the Window under Netscape; under IE, you need to go deep into the Document to detect the body; In the DOM environment, to get the size of the window, you need to pay attention to the size of the root element, not the element.
The innerWidth property of the Window object contains the inner width of the current window. The innerHeight property of the Window object contains the inner height of the current window.
The body property of the Document object corresponds to the tags of the HTML document. The documentElement property of the Document object represents the root node of the HTML document.
document.body.clientHeight represents the current height of the window where the HTML document is located. document.body.clientWidth represents the current width of the window where the HTML document is located.

implementation code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Please adjust the browser window</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<h2 align="center">Please resize the browser window</h2><hr>
<form action="#" method="get" name="form1" id="form1">
<!--Display the actual size of the browser window-->
Actual height of browser window: <input type="text" name="availHeight" size="4"><br>
Actual width of browser window: <input type="text" name="availWidth" size="4"><br>
</form>
<script type="text/javascript">
<!-- 
var winWidth = 0;
var winHeight = 0;
function findDimensions() //function: get dimensions
{
//get window width
if (window.innerWidth)
winWidth = window.innerWidth;
else if ((document.body) && (document.body.clientWidth))
winWidth = document.body.clientWidth;
//get window height
if (window.innerHeight)
winHeight = window.innerHeight;
else if ((document.body) && (document.body.clientHeight))
winHeight = document.body.clientHeight;
//Get the window size by detecting the body inside the Document
if (document.documentElement  && document.documentElement.clientHeight && document.documentElement.clientWidth)
{
winHeight = document.documentElement.clientHeight;
winWidth = document.documentElement.clientWidth;
}
// output the result to two text boxes
document.form1.availHeight.value= winHeight;
document.form1.availWidth.value= winWidth;
}
findDimensions();
//call the function to get the value
window.onresize=findDimensions;
//-->
</script>
</body>

</html> 

Source program interpretation

(1) The program first creates a form, which contains two text boxes, which are used to display the current width and height of the window, and its value will change with the size of the window.
(2) In the following JavaScript code, two variables winWidth and winHeight are first defined to save the height and width of the window.
(3) Then, in the function findDimensions ( ), use window.innerHeight and window.innerWidth to get the height and width of the window, and save them in the aforementioned two variables.
(4) Detect the body by going deep into the Document, get the window size, and store it in the aforementioned two variables.
(5) At the end of the function, by accessing form elements by name, the results are output to two text boxes.
(6) At the end of the JavaScript code, complete the entire operation by calling the findDimensions ( ) function.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326214613&siteId=291194637