This article mainly introduces relevant information about the difference between p's offsetLeft and style.left. Friends who need it can refer to it
clientX event attribute returns When the event is triggered, the mouse pointer points to the browser page ( or client area) horizontal coordinates.
The client area refers to the current window.
If the position of the parent p is defined as relative and the position of the child p is defined as absolute, then the value of style.left of the child p is relative to the value of the parent p, which 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 more convenient to use offsetLeft.
2. style.left is read-write, offsetLeft is read-only, so to change the position of p, 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 done experiments. If it is defined in css, the value of style.left is still empty. This is the problem I encountered at the beginning. I can't always get the value of style.left. offsetLeft can still be obtained without defining the position of p in advance.
The above is the detailed content of Compare the usage differences between offsetLeft and style.left in div. For more information, please follow other related articles on the PHP Chinese website!