JS Quick Solution to Get Null style. left Values for Floating of float Elements

  • 2021-07-22 08:40:45
  • OfStack

Solution:

1. Set the top and left values of elements using inline styles;

2. or directly obtains the offsetLeft of the element to obtain the relevant value, and parseInt is not needed

Cause of problem:

If position of parent div is defined as relative and position of child div is defined as absolute, then the values of style. left of child div are the same as those of parent div, except that:

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.

style. left is read-write, and offsetLeft is read-only, so to change the position of div, you can only modify style. left.

The value of style. left needs to be defined in advance, otherwise the obtained value is empty and must be defined in the form of inline style. If it is defined in css, the value of style. left is still undefined; However, offsetLeft can still be retrieved without defining the location of div in advance.

Above is this site to introduce you JS to get floating (float) elements of style. left value for the null quick solution, I hope to help you, if you have any questions welcome to leave me a message, this site will reply to you in time!


Related articles: