This article is a mirror article of machine translation, please click here to jump to the original article.

View: 9422|Reply: 0

[JavaScript] The difference between offsetLeft and style.left

[Copy link]
Posted on 2/18/2019 2:05:39 PM | | |
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.





Previous:jQuery effect - toggle() method
Next:jQuery on() method
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com