Common:
JS Get browser window size
// 获取窗口宽度
if (windows.innerWidth) winWidth = windows.innerWidth; else if ((document.body) && (document.body.clientWidth)) winWidth = document.body.clientWidth; // 获取窗口高度
if (windows.innerHeight) winHeight = windows.innerHeight; else if ((document.body) && (document.body.clientHeight)) winHeight = document.body.clientHeight; // 通过深入 Document 内部对 body 进行检测,获取窗口大小
if
(document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {
winHeight = document.documentElement.clientHeight; winWidth = document.documentElement.clientWidth; } |
With:
About getting the various browser visible window sizes:
In my local test:
Can be used under IE, FireFox, opera
Document.body.clientWidth
Document.body.clientHeight
Can be obtained, very simple, very convenient.
And in the company project:
Opera still uses
Document.body.clientWidth
Document.body.clientHeight
But IE and Firefox use
Document.documentElement.clientWidth
Document.documentElement.clientHeight
It's the standard of the "the".
If you add this line tag to the page in IE:
Document.body.clientWidth ==> Body Object width
Document.body.clientHeight ==> Body Object Height
Document.documentElement.clientWidth ==> Visible Area width
Document.documentElement.clientHeight ==> Visible Area height
In Firefox:
Document.body.clientWidth ==> Body Object width
Document.body.clientHeight ==> Body Object Height
Document.documentElement.clientWidth ==> Visible Area width
Document.documentElement.clientHeight ==> Visible Area height
?
In Opera:
Document.body.clientWidth ==> Visible Area width
Document.body.clientHeight ==> Visible Area height
Document.documentElement.clientWidth ==> The width of the page object (that is, the body object width plus the margin width)
Document.documentElement.clientHeight ==> Page Object height (i.e. body object height plus margin height)
And if there is no standard to define the
IE is:
Document.documentElement.clientWidth ==> 0
Document.documentElement.clientHeight ==> 0
Firefox is:
Document.documentElement.clientWidth ==> Page object width (i.e. body object width plus margin) Document.documentElement.clientHeight = = > Page object height (i.e. body object height plus margin)
Opera is:
Document.documentElement.clientWidth ==> Page object width (i.e. body object width plus margin) Document.documentElement.clientHeight = = > Page object height (i.e. body object height plus margin)
ScrollHeight: Gets the scroll height of the object.
ScrollLeft: Sets or gets the distance between the left edge of the object and the leftmost of the currently visible content in the window
ScrollTop: Sets or gets the distance between the top of the object and the top of the visible content in the window
ScrollWidth: Gets the scrolling width of the object
Offsetheight: Gets the height of the object relative to the layout or parent coordinates specified by the parent coordinate OffsetParent property
Offsetleft: Gets the calculated left position of the object relative to the layout or the parent coordinate specified by the OffsetParent property
OffsetTop: Gets the computed top position of the object relative to the layout or the parent coordinate specified by the OffsetTop property
Event.clientx horizontal coordinates of relative documents
Event.clienty vertical coordinates of relative documents
Event.offsetx horizontal coordinates relative to the container
Event.offsety the vertical coordinate of the relative container
Document.documentElement.scrollTop The vertical scrolling value
Event.clientx+document.documentelement.scrolltop horizontal coordinates of relative documents + amount of vertical scrolling
The above mainly refers to IE, Firefox differences are 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 mention: the Margin property in CSS, and clientwidth, Offsetwidth, clientheight, offsetheight are irrelevant)
Assume that obj is an HTML control.
Obj.offsettop refers to the calculation of the upper-side position of obj relative to the layout or the parent coordinate specified by the OffsetParent property, Integer, per pixel.
Obj.offsetleft refers to the left position, integer, Unit pixel, of the calculated obj relative to the layout or the parent coordinate specified by the OffsetParent property.
Obj.offsetwidth refers to the absolute width of the obj control itself, excluding parts that are not displayed because of overflow, that is, its actual occupied width, integer type, unit pixels.
Obj.offsetheight refers to the absolute height of the obj control itself, excluding parts that are not displayed because of overflow, that is, the height that it actually occupies, the integer type, and the unit pixels.
Let's make a note of the offsetParent mentioned earlier.
OffsetParent gets a reference to the container object that defines the OffsetTop and Offsetleft properties of the object. OffsetTop and offsetParent are very complex, different browsers have different interpretations, floating the explanation is different, so we generally as long as the understanding through the two can get control in the absolute position in the browser.
The above properties are also available in FireFox.
Also: What we're talking about here is the property value of the HTML control, not the value of the document.body,document.body is interpreted differently in different browsers (in fact most environments are caused by different interpretations of document.body, not because Offset interpretation of different causes)
We know that OffsetTop can get the position of the HTML element from above or outside the element, and Style.top is also possible, the difference is:
First, OFFSETTOP returns a number, and Style.top returns a string, in addition to the number of units: px.
Second, OffsetTop read only, and Style.top can read and write.
If the top style is not specified for the HTML element, Style.top returns an empty string.
Offsetleft and Style.left, offsetwidth and Style.width, offsetheight and Style.height are the same.
ClientHeight
Everyone has no objection to the clientheight, are considered to be the height of the content viewable area, that is, the page browser can see the content of the height of the area, is generally the last toolbar below the status bar above the area, not related to the content of the page.
Offsetheight
IE, Opera thinks offsetheight = clientheight + scroll bar + border.
NS, FF think offsetheight is the actual height of the Web content, can be less than clientheight.
ScrollHeight
IE, Opera think ScrollHeight is the actual height of the Web content, can be less than clientheight.
NS, FF thinks that scrollheight is the height of the Web content, but the minimum value is clientheight.
To put it simply
ClientHeight is the height of the area through which the content is viewed by the browser.
NS, FF think Offsetheight and scrollheight are Web content height, but when the content height of the page is less than or equal to ClientHeight, scrollheight value is clientheight, and offsetheight Can be less than clientheight.
IE, Opera thinks offsetheight is the visible area clientheight scroll bar and border. ScrollHeight is the actual height of the Web content.
Similarly
The explanations of clientwidth, offsetwidth and ScrollWidth are the same as above, just change the height to width.
Description
The above is based on the DTD HTML 4.01 Transitional, if the DTD XHTML 1.0 transitional The meaning will be different, in XHTML, these three values are the same value, both represent the actual height of the content. Most new versions of browsers support different interpreters based on the DOCTYPE specified on the page
ScrollTop is the "volume" Height value, example:
<div style= "Width:100px;height:100px;overflow:hidden;" id= "P" >
<div style= "width:50px;height:300px;" id= "T" > If scrolltop is set for p, the content may not be fully displayed. </div>
</div>
<script type= "Text/javascript" >
var p = document.getelementbyidx_x ("P");
P.scrolltop = 10;
</script>
Because ScrollTop is set for the outer element p, the inner element is rolled up, and the part that is ScrollTop is the volume.
ScrollLeft is a similar truth.
We already know that offsetheight is the width of its own element, while ScrollHeight is the absolute width of the inner element, which contains the hidden part of the inner element. The scrollheight of p above is 300, while P's offsetheight is 100.
ScrollWidth is a similar truth.
IE and FireFox are fully supported, while Netscape 8 and Opera 7.6 Do not support scrolltop, scrollleft (except Document.body.scrollTop, Document.body.scrollLeft).
1.clientHeight, ClientWidth:
These two properties generally show the pixel height and width of the element's content. Theoretically, these measurements are not considered for any addition of the style sheet
The page margins, borders, and so on in the element.
2.clientleft,clienttop:
These two return the thickness of the border around the element, and if you do not specify a border or do not position the element, his value is 0.
3.scrollleft,scrolltop:
If the elements can be scrolled, you can get how far the elements are scrolled in both horizontal and vertical directions, in pixels, by these two properties.
For elements that cannot be scrolled, these values are always 0.
4.scrollheight,scrollwidth:
No matter how many objects are visible on the page, they get the whole.
5.style.left:
Offset of the left edge of the rectangle containing the anchor element
6.style.pixelleft:
Returns the integer pixel value of the left boundary offset of the anchored element. Because the non-pixel value of the property returns a string containing the unit, for example, 30px. This property allows you to handle numeric values in pixels individually.
7.STYLE:POSLETF:
Returns the quantity value of the left boundary offset of the anchored element. Regardless of the unit specified by the corresponding style sheet element. Because the non-positional value of the property returns a string containing the unit, for example, 1.2em
Top,pixeltop, Postop a few analogies on the line. The
left: is the left-to-right position, which is the distance from the left edge of the screen;
clientleft returns the distance between the Offsetleft property value of the object and the true value to the left of the current window
offsetleft Returns the left value of the layout or coordinates of the object relative to the parent object, which is the coordinate origin of the parent object, the x-coordinate to the right and down for the X-, y-axis positive direction,
pixelleft Sets or returns the position of the object relative to the left of the window
ScrollWidth is the width of the actual content of the object, without the width of the edge, which changes depending on how much content is in the object (the actual width of the object may be changed by more content). The
ClientWidth is the width of the object visible, without edges such as scroll bars, which change with the window's display size. The
offsetwidth is the visible width of the object, and edges such as the package scroll bar change with the window's display size.
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 mention: Margin property in CSS, with clientwidth, offsetwidth, ClientHeight, offsetheight are not relevant)
Offsetwidth: Is the offset width of the element relative to the parent element. equals Border+padding+width
ClientWidth: Is the visible width of the element. equals Padding+width
ScrollWidth: Is the width of the element and includes the scrolling part.
The position of the offsetleft:html element relative to its own offsetparent element
ScrollLeft: Returns and sets the coordinate value of the current horizontal scrolling service
<input type= "button" value= "click" onclick= "Move ()" >
<div id= "D" style= "Position:absolute; left:170px; Top:100px;width:300;height:300;overflow:scroll "
onclick= "alert (' offsetleft: ' +this.offsetleft)" >
<div style= "height:600;width:600" onclick= "alert (' offsetleft: ' +this.offsetleft) ' ></div>
</div>
<script language= "JavaScript" >
function Move ()
{
var d=document.getelementbyidx_x ("D")
A=eval_r (20)
D.scrollleft+=a
}
</script>
Save as Web page, run, click button, scroll bar to move
Click the div to pop the position of b relative to a, then the position of a relative to the window.
JS get browser window size clientwidth, offsetwidth, scrollwidth