Concepts of some common control DIV in JS (important)

Source: Internet
Author: User

Div. style. Left; available only when the position attribute of the object is set. Otherwise, this property setting will be ignored.
Div. style. Top; available only when the position attribute of the object is set. Otherwise, this property setting will be ignored.
Div. style. Border; Edge Width
Div. style. margin; the distance between the parent Div and its outer Div
Div. style. padding; distance between parent Div and content
Div. offsetleft; the left-side position of OBJ relative to the layout or the parent coordinate specified by the offsetparent attribute. It is an integer in pixels.
Div. offsetheight: the absolute height of the OBJ control, excluding the undisplayed parts due to overflow, that is, the actual height occupied by the control, which is an integer in pixels.
Div. offsettop; obj is relative to the layout or the parent coordinate specified by the offsetparent attribute. It is an integer in pixels.Offsettop read-only
Div. offsetwidth; refers to the absolute width of the OBJ control, which does not include the portion not displayed due to overflow, that is, the actual width, integer, or unit pixel.
Div. clientleft;
Div. clientheight; is regarded as the height of the visible area of the content. That is to say, the height of the area where the content can be viewed in the browser of the page is generally the area where the last toolbar is below the status bar, it has nothing to do with the page content.
Div. clientwidth;
Div. clienttop;
Div. scrolltop; scrolltop refers to the height of the section where "content in an element" exceeds the "upper boundary of an element.
Div. scrollheight; is the height of the element and includes the scroll part (that is, the part that is not displayed)
Div. scrollwidth; is the width of the element and includes the scroll part.

 

1. OBJ. offsettop

OBJ. offsetleft

OBJ. offsetwidth

OBJ. offsetheight

These are only used to read the positioning value read-only, but must be set through document. getelementbyid ("test"). style. marginleft = 200;

 

2scrolltop refers to the height of the section where "content in the element" exceeds the "element upper boundary.

Http://blog.csdn.net/gang_gang_gang/archive/2009/06/01/4233044.aspx

 

3

Offsetwidth: the offset width of the element relative to the parent element. Equal to border + padding + width
Clientwidth: the visible width of the element. Equal to padding + width
Scrollwidth: Specifies the width of an element and includes the scrolling part.

 

4

. Top

This attribute is only available when the position attribute of an object is set. Otherwise, this attribute is ignored.

 

< Div Style = "Background-color: red; position: absolute; width: 100px; Height: 100px ;" >

<PStyle= "Background-color: Silver; position: absolute; top:-5px ;">Test top</P>

</Div>

 

 

Above is a section P included in a div. You can see that after P's top is set to-5px, its top margin exceeds the top margin of the container Div, the above distance is the set 5px.

Note that both Div and P must be set to absolute to get the desired result. If the parent element is not set, then, the reference of the child element will be the element that has defined position on the upper layer until the entire document;

2. postop

The value of postop is actually the same as that of top, but the difference is that the unit of top elements is fixed to PX, postop is only a numerical value (you can use alert ("Top =" + id. style. top) and alert ("postop =" + id. style. so postop is generally used for calculation.

 

< Div Style = "Background-color: red; position: absolute; width: 100px; Height: 100px ;" >

<PID= "Test"Style= "Background-color: Silver; position: absolute ;">Test postop</P>

</Div>

< Script >
Test. style. postop
= 15 + 8 ;
Alert (
" Top = " + Test. style. Top );
Alert (
" Postop = " + Test. style. postop );
</ Script >

 

 

Whether you use top or postop to assign values, the final results are consistent.

3. scrolltop

 

< Div ID = "Container" Style = "Background-color: Silver; width: 100px; Height: 100px; overflow: auto ;" >
< P Style = "Background-color: red ;" >
Don't be a lover, a cat, a dog, a lover, a pet, at least cute, charming, and you don't have to worry about it. Tomorrow will be yours.
</ P >
</ Div >

<Script>
Container. scrolltop
= 12;
</Script>

 

 

This section of text cannot be fully displayed in the DIV of 100*100. Therefore, if overflow is set to auto, a sliding box in the upper and lower directions appears. If no ID is set. the slider is located at the top by default. After the scrolltop value is set to 12, the position of the slider is changed. By default, the text that has been written in 12 pixels is displayed. If overflow is set to hidden, the top 12 pixels of text cannot be displayed.

Note that the setting method is ID. scrolltop, instead of ID. style. scrolltop.

4. scrollheight and offsetheight

Offsetheight is the height of its own elements, and scrollheight is the height of its own elements + the height of hidden elements.

 

< Div ID = "Container" Style = "Background-color: Silver; width: 100px; Height: 100px; overflow: auto ;" >
< P Style = "Background-color: red; Height: 250px ;" >
Don't be a lover, a cat, a dog, a lover, a pet, at least cute, charming, and you don't have to worry about it. Tomorrow will be yours.
</ P >
</ Div >

script >
alert (container. offsetheight);
alert (container. scrollheight);
script >< /Span>

100,250 is output in sequence. Because the height of an element has been specified to be 100px, offsetheight is always 100px; the internal element is 250px, and the container element is only 100px, so there is a 150px content that cannot be displayed, but it actually exists, so the scrollheight value is 100 + 150 = 250.

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.