Tutorials on top, clienttop, ScrollTop, offsettop in JavaScript

Source: Internet
Author: User

Source: http://www.cnblogs.com/trlanfeng/archive/2012/11/04/2753280.html

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)

onmouseover when the mouse passes, trigger = the script function behind

onmouseout when the mouse leaves.

OnMouseUP when the mouse is pressed and lifted ...

OnMouseMove when the mouse moves.

OnMouseDown when the mouse is pressed ...

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.getElementById ("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 value of the number 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.

Left: The distance between the left and right 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 x-coordinate of the X-, y-axis positive direction of the parent object, the upper-right corner of the parents as the coordinate origin.

Pixelleft Sets or returns the position of the object relative to the left side 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).

ClientWidth is the width of the object visible, without edges such as the scroll bar, which changes with the window's display size.

The offsetwidth is the visible width of the object, and the 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: the Margin property in CSS, and clientwidth, Offsetwidth, clientheight, offsetheight are irrelevant)

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.getelementbyid ("D")

A=eval (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.

OffsetTop, Offsetleft, offsetwidth, offsetheight

is equal to the rotation bar pulling interval in the case of a longitudinal rotation bar emerges.

NS, FF think offsetheight is the real height of web content, can be less than clientheight.

FF ClientHeight in the DOCTYPE of the differences, XHTML 1 trasitional is not described above. No other reader exists for this topic.

The width of the current tool.

Note that if the tool is included in a Div, this div will not be treated as a parent layer of the tool (that is, the parent layer of the tool skips the Div tool) and the parent layer is table without a problem.

3. If the top style is not specified for the HTML element, Style.top returns an empty string.

IE and FireFox are well supported, while Netscape and Opera do not support ScrollTop, scrollleft (except Document.body).

6.scrollLeft:

5.offsetParent:

Page body Full text High: document.body.scrollHeight;

NS, FF think offsetheight is the real height of web content, can be less than clientheight.

Offsetheight

But

Other: What we are talking about is the HTML control's property value, in fact, is not the value of the document.body,document.body in the divergent reader there are differences in the explanation (the reality of the last night most of the situation is because of the document.body explanation of differences caused, in fact, not because of the Offset to explain the differences caused), click here to examine the differences point.

The OffsetTop of the Submit button is the interval between the "Submit" button and the top border of the "tool" layer, because the top frame of the "tool" layer is compared to its top.

var top = 0;

These four types of readers are separated into IE (Internet Explorer), NS (Netscape), Opera, FF (FireFox).

ScrollHeight

Web page is rolled toward High: document.body.scrollTop;

The parent layer tool for the current tool.

We already know that offsetheight is the width of its own elements.

Here we talk about the four kinds of readers of the Document.body clientheight, offsetheight and scrollheight description, here is document.body, if it is HTML control, then there are differences, click here to inspect.

2, ClientHeight

The above properties are also useful in FireFox.

2, OffsetTop read only, and Style.top can read and write.

top = Obj.offsettop;

Copy the code code below:

Page Visible area width: document.body.clientWidth;

Get the best position script code

ClientHeight

Similarly

Obj.offsetwidth refers to the width, integer, and cell pixels of the obj control itself.

Similarly

The interval to the left of the current tool to its parent layer.

The difference with the Style.height property is that if the width of the tool is set to a percentage height, Style.height returns this percentage regardless of the page size, and offsetheight returns the height value of the tool in the divided page instead of the percent value

Obj.offsetheight refers to the height, integer, and cell pixels of the obj control itself.

while (obj! = document.body)

Page body part left: window.screenleft;

2.offsetLeft:

We know that OffsetTop can get the status of elements above or outside HTML elements, Style.top is also possible, the difference is:

These four types of readers are separated into IE (Internet Explorer), NS (Netscape), Opera, FF (FireFox).

7.scrollTop

Page visible Area High: Document.body.offsetHeight (contains the width of the edge);

The big family to clientheight all have no two words, all thought is the content to see the height of the area, that is to say the page reader can be seen the content of the ┞ of the height of the area, as usual is the last object bar below to the status bar above the ┞ area, not related to the page content.

ClientHeight

Say it in a simple way

The offsetleft of the submit button refers to the interval between the Submit button and the left border of the tool layer, because the left border of the tool layer is compared to the left side of the toolbar.

Say it in a simple way

High screen resolution: window.screen.height;

Title: ScrollTop, ScrollLeft, ScrollWidth, scrollheight

Obj.offsetleft refers to the position, integer, and cell pixels of the left or upper control of the obj interval.

IE, Opera think ScrollHeight is the real height of web content, can be less than clientheight.

1.offsetTop:

The scrollheight of p above is 300, while P's offsetheight is 100.

By applying this property, you can get the right position of the current tool in the most divergent pages.

}

Prepare common sense: OffsetTop, offsetleft, offsetwidth, offsetheight

IE, Opera thought offsetheight = clientheight + rotation bar + border.

The above is based on the DTD HTML 4.01 Transitional, if it is the DTD XHTML 1.0 transitional The meaning will be divided, in XHTML, these three values are uniform values, all of the actual height of the content. The new version of the reader supports the DOCTYPE to enable the divergence specifier based on the specified page. Download or read the test file.

If ScrollTop is set for p, the content will probably not be fully represented.

Title: The difference between OffsetTop and style.top

Assume that obj is an HTML control.

var left = 0;

Example:

}

3, ScrollLeft

ClientHeight is to look at the content of ┞ through the reader to seal the height of the area.

Page visible Area High: document.body.clientHeight;

IE, Opera think offsetheight is visible area clientheight rotation bar and border. ScrollHeight is the real height of web content.

Screen available things area width: window.screen.availWidth;

Title: ClientHeight, Offsetheight and ScrollHeight

4.offsetHeight:

ClientWidth, offsetwidth and ScrollWidth's instructions are identical to the above, just change the height to width.

Alert ("Left are:" + Left + "\ r \ n" + "Top is:" + top);

obj = obj.offsetparent;

Web page Visible Area width: Document.body.offsetWidth (contains the width of the edge);

IE, Opera think ScrollHeight is the real height of web content, can be less than clientheight.

IE, Opera thought offsetheight = clientheight + rotation bar + border.

1, OffsetTop return is a number, and Style.top returns a string, in addition to the number of cells: px.

Page body Full text width: document.body.scrollWidth;

P.scrolltop = 10;

4, ClientLeft

We explain the above or above and the left or upper controls as mentioned above.

Category: Javascript

Tutorials on top, clienttop, ScrollTop, offsettop in JavaScript

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.