offsetLeft gets the left margin relative to the parent object
left gets or sets the left margin relative to the parent object with the positioning property (position is defined as relative).
If the position of the parent div is defined as: relative: Generate an absolutely positioned element
, the position of the subdiv is defined as absolute:Generate relative positioned elements , then the value of style.left of the child div is relative to the value of the parent div,
This is the same as offsetLeft, except that: 1. style.left returns a string, such as 28px, offsetLeft returns a value of 28, if you need to calculate the obtained value, It is also more convenient to use offsetLeft. 2. style.left is read-write, offsetLeft is read-only, so to change the position of the div, you can only modify style.left. 3. The value of style.left needs to be defined in advance, otherwise the value obtained will be empty. And it must be defined in html, I have experimented, if it is defined in In CSS, the value of style.left is still empty, which is the problem I encountered at the beginning, and I always can't get the value of style.left.
offsetLeft can still be retrieved without having to define the div position beforehand.
|