Document.documentelement and Document.body usage differences in JS

Source: Internet
Author: User

Gets the height of the scroll bar in the Web page, which can be obtained by DOCUMENT.BODY.SCROLLTOP, such as making div scroll along the scroll bar:

The code is as follows Copy Code

<div id= "div" style= "width:100px;height:100px;background: #ccc;p osition:absolute;" ></div>
Window.onscroll = function ()
{
var div = document.getElementById ("div");
Div.style.top = Document.body.scrollTop + "px";
}

After the operation did not achieve the desired effect, output document.body.scrollTop value A look, has always been 0. A toss, the original is a DTD problem, if the page directly with the

If you use a DTD, then use Document.documentElement.scrollTop instead of Document.body.scrollTop.

The code is as follows Copy Code

Window.onscroll = function ()
{
var ofix = document.getElementById ("Divfix");
OFix.style.top = Document.documentElement.scrollTop + "px";
}

DTD Related notes:


The page has a DTD, or when DOCTYPE is specified, use Document.documentelement.

The page does not have a DTD, or DOCTYPE is not specified, use Document.body.

This is true in both IE and Firefox.

For compatibility, whether or not there is a DTD, you can use the following code:

The code is as follows Copy Code

var scrolltop = window.pageyoffset//For FF
|| Document.documentElement.scrollTop
|| Document.body.scrollTop
|| 0;


DocumentElement and Body related instructions:


The body is the body child node in the DOM object, that is, the <body> tag;

DocumentElement is the root node of the entire node tree, that is, the

Dom refers to each object in the hierarchy as a node, is a hierarchy, you can understand as a tree structure, like our directory, a root directory, the root directory has subdirectories, subdirectories also have subdirectories.

Take HTML Hypertext Markup Language as an example: one root of the entire document is that the DOM


Get the individual properties as follows:

Web page Visible area wide: document.body.clientWidth;
Page visible Area High: document.body.clientHeight;
Web page Visible area wide: document.body.offsetWidth (including the width of the sideline);
Page visible Area High: document.body.offsetHeight (including the width of the sideline);
Web page Full text width: document.body.scrollWidth;
The full text of the Web page is high: document.body.scrollHeight;
The Web page is rolled up high: document.body.scrollTop;
The pages are rolled away left: document.body.scrollLeft;
The main body part of the Web page: window.screentop;
Page body part left: window.screenleft;
High screen resolution: window.screen.height;
Width of screen resolution: Window.screen.width;
Screen available workspace height: window.screen.availHeight;
Screen available working area width: window.screen.availWidth;


ScrollHeight: Gets the scrolling height of the object.
ScrollLeft: Sets or gets the left distance between the left edge of an object and the current visible content in the window
ScrollTop: Sets or gets the distance between the top of the object and the topmost part 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 coordinates specified by the Offsetparent property
offsettop: Gets the calculated top position of the object relative to the layout or the parent coordinates specified by the offsettop property
Event.clientx the horizontal coordinates of the relative document
Event.clienty vertical coordinates relative to document

The horizontal coordinates of the event.offsetx relative to the container
The vertical coordinates of the event.offsety relative to the container
Document.documentElement.scrollTop The vertical scrolling value
Event.clientx+document.documentelement.scrolltop relative to the document's horizontal coordinates + vertical scrolling amount
Post by Molong on 2009-05-19 11:57 PM #1

To get the scroll bar ordinate position of the current page, use:

The code is as follows Copy Code
Document.documentElement.scrollTop;
Instead of:
Document.body.scrollTop;

DocumentElement corresponds to the HTML tag, and the body corresponds to the body tag.

Under the standard, the DOCUMENT.BODY.SCROLLTOP constant is 0, which needs to be replaced by DOCUMENT.DOCUMENTELEMENT.SCROLLTOP;
If you want to position the mouse relative to the absolute position of the page, you will find that there are 999.99 articles in 1000 posts in Google that will let you use event.clientx+document.body.scrollleft,event.clienty+ Document.body.scrollTop, if you find that your mouse position deviates from your imagination, don't be surprised, this is a normal thing.
The Document.body.scrollX object has not been supported after ie5.5.
So when you're programming, please add the same kind of judgment

  code is as follows copy code
if ( Document.body && document.body.scrollTop && document.body.scrollLeft)
{
    Top=document.body.scrolltop;
    left=document.body.scrollleft;    
}
if ( Document.documentelement && Document.documentElement.scrollTop && Document.documentElement.scrollLeft)
{
    top=document.documentelement.scrolltop;
    Left=document.documentelement.scrollleft;
}

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.