Skip to content
Advertisement

Convert 45 to 1 and 345 to 2 [closed]

I’m building a carousel with a swiping limit exceeding both left and right of 45px on each item has 300px width I wonder How to convert 45 to 1 index and 345 to 2 index. I have a total of 3 items for the carousel.

I have the following formula for converting it to 2 indexes however not on the first index

If the current position is on 100px which is greater than 45 it should still result 1 index

(345 / 345 * 2) a result of 2

(45 / 345 * 2) a result of 0.2608695652173913

I’m trying to make that one line

JavaScript

Carousel for mobile screens

Advertisement

Answer

Try using this one:

JavaScript

In your specific source code, it would be something like this:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement