Div box model (Figure 1), div Box Model
OffsetLeft obtains the left margin relative to the parent object.
Left gets or sets the left margin relative to the parent object with the position attribute (position defined as relative ).
If the position of the parent div is defined as relative and the position of the child div is defined as absolute, the style. left value of the Child div is relative to the parent div value,
This is the same as offsetLeft. The difference is:
1. style. left returns a string, such as 28px, and offsetLeft returns a value of 28. If you need to calculate the obtained value,
It is also convenient to use offsetLeft.
2. style. left is read/write, and offsetLeft is read-only. To change the position of the div, you can only modify style. left.
3. The value of style. left must be defined in advance. Otherwise, the obtained value is null. And must be defined in html. I have tried it. If it is defined in
In css, the value of style. left is still empty, which is the problem I encountered at the beginning. The value of style. left is always not available.
OffsetLeft can still be obtained without the need to define the position of the div in advance.