zillow / react-slider

Accessible, CSS agnostic, slider component for React.
https://zillow.github.io/react-slider
MIT License
883 stars 231 forks source link

Thumb Incorrect positioning #302

Open Galy4a opened 10 months ago

Galy4a commented 10 months ago

Description

Hello! I noticed that the formula for calculating the position of the thumb in the calcOffset method is incorrect. The width of the thumb is subtracted from the width of the bar before being multiplied by the ratio, when the correct calculation would be to first multiply the ratio by the width of the bar, and then subtract half the width of the thumb.

Eg:

Block width 400px, thumb width 16px, selection range 0-100. If the thumb is at 40, then its position will be equal to 400 ((40 - 0) / 100) - (16 / 2) = 400 0.4 - 8 = 160 - 8 = 152px; According to the current formula: (400 - 16) ((40 - 0) / 100) = 384 0.4 = 153.6;

The values ​​are very close and the error is difficult to see with the naked eye. The correct position is calculated only for the middle of the bar, before and after it will be a little more or a little less, respectively

Edit zillow/react-slider

msf-caesar commented 10 months ago

Try with multiplying the ratio by the width of the bar and then subtract half the width of the thumb. This will correctly calculate the position of the thumb on the slider.