The clientheight and ClientWidth attributes of the document element represent the size of the Web page.
The code is as follows |
Copy Code |
function GetViewport () { if (Document.compatmode = = "Backcompat") { return { Width:document.body.clientWidth, Height:document.body.clientHeight } } else { return { Width:document.documentElement.clientWidth, Height:document.documentElement.clientHeight } } } |
The GetViewport function above can return the height and width of the browser window
There are three places to note:
1 This function must be completed after the page is loaded to run, otherwise the document object has not been generated, the browser will complain.
2 In most cases, the Document.documentElement.clientWidth returns the correct value. However, in the IE6 quirks mode, Document.body.clientWidth returns the correct value, so the function adds a judgment to the document pattern.
3 clientwidth and ClientHeight are read-only properties and cannot be assigned values.
Get the absolute position of a page element
The following two functions can be used to get the horizontal and vertical coordinates of the absolute position.
The code is as follows |
Copy Code |
function Getelementleft (Element) { var actualleft = Element.offsetleft; var current = Element.offsetparent; while (current!== null) { Actualleft + = Current.offsetleft; current = Current.offsetparent; } return actualleft; } function Getelementtop (Element) { var actualtop = element.offsettop; var current = Element.offsetparent; while (current!== null) { Actualtop + = Current.offsettop; current = Current.offsetparent; } return actualtop; } |
Because the Offsetparent object is not necessarily equal to the parent container in tables and IFRAME, the above function does not apply to elements in tables and IFRAME.
Some get element position/window height code
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 ==> Page Object width (that is, the body object width plus margin width)
Document.documentElement.clientHeight ==> Page Object height (that is, height of the body object plus margin high)
If the standard of the consortium is not defined, the
IE is:
Document.documentElement.clientWidth ==> 0
Document.documentElement.clientHeight ==> 0
Firefox is:
Document.documentElement.clientWidth ==> Page object width (that is, body object width plus margin width) Document.documentElement.clientHeight = = > Page Object height (that is, the body object height plus margin high)
Opera is:
Document.documentElement.clientWidth ==> Page object width (that is, the body object width plus margin width) document.documentElement.clientHeight ==> Page Object height (that is, the body object height plus margin high)
It's a hassle, In fact, in terms of development, rather fewer objects and methods, do not use the latest standards to facilitate a lot of AH.