Offsettop, offsetwidth, offsetparent

Source: Internet
Author: User

The methods of offsetleft, offsettop, scrollleft, and scrolltop have been very confusing for a long time. It takes a day to learn about them and the following results are obtained:
1. offsettop:
The distance from the current object to the top of the upper layer.
You cannot assign a value to the object. Use the style. Top attribute to set the distance from the object to the top of the page.

2. offsetleft:
The distance from the current object to the left of its upper layer.
You cannot assign a value to the object. Use the style. Left attribute to set the distance from the object to the left of the page.

3. offsetwidth:
The width of the current object.
And style. the difference between the width attribute is that, for example, if the width of an object is set to the percentage width, the style is displayed regardless of whether the page is larger or smaller. both width return this percentage, while offsetwidth returns the width value of the object in different pages instead of the percentage value.

4. offsetheight:
And style. the difference between the height attribute is that, for example, if the object width is set to the percentage height, the style is displayed regardless of whether the page is larger or smaller. height returns this percentage, while offsetheight returns the object height value on different pages, instead of the percentage value.

5. offsetparent:
The upper-level object of the current object.
Note. if the object is included in a DIV, this Div will not be treated as the parent layer of the object (that is, the parent layer of the object will skip the DIV object) when the upper layer is a table, no problem occurs.
This attribute can be used to obtain the absolute position of the current object on pages of different sizes.

Function getposition (OBJ)
...{
VaR left = 0;
VaR Top = 0;

While (OBJ! = Document. Body)
...{
Left = obj. offsetleft;
Top = obj. offsettop;

OBJ = obj. offsetparent;
}

Alert ("Left is:" + Left + "" + "Top is:" + TOP );
}

6. scrollleft:
The leftmost distance from the object to the left of the object in the range displayed in the current window.
That is, the distance pulled by the scroll bar when a horizontal scroll bar appears.

7. scrolltop
The distance from the top of the object to the top edge of the object in the range displayed in the current window.
That is, the distance pulled by the scroll bar when a vertical scroll bar appears.

**************************************** **************************************** **********************************

Offsetleft: Location of the HTML element relative to its offsetparent Element

Scrollleft: returns and sets the coordinates of the current Horizontal Rolling task.

<Input type = "button" value = "click" onclick = "move ()">
<Div id = "D" style = "background-color: # ff9966; 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 + =
}
</SCRIPT>

Save as webpage, run it, click the button, and move the scroll bar
Click Div. The position of B relative to a is displayed first, and then the position of a relative window is displayed.

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.