JavaScript gets element position/window height code

Source: Internet
Author: User

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.

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.