H
H
high-draw2021-10-25 11:21:06
css
high-draw, 2021-10-25 11:21:06

How to calculate slider padding based on screen width?

Making a self-written javascript slider

How to calculate slider padding depending on the screen width?
The size of the mobile device can be from 320 to 640. The screenshot shows both of these extreme options - 320 and 640.
In general, you need the second photo to always be in the center of the screen. How to calculate block shift in photos depending on screen width? I need a formula, I don't need javascript, I'll write it myself.

617668d54690b425614054.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Romankov, 2021-11-04
@romant094

Something like this:

(slideWidth + gapBetweenSlides) * activeIndex + (screenWidth - slideWidth) / 2

where
slideWidth - slide width
gapBetweenSlides - distance between slides
activeIndex - active slide
index screenWidth - screen width at the time the script was run
Sketched on the knee without a test. Maybe something needs to be tweaked.
The solution, taking into account the fact that there is a horizontal scroll and the position of the child is calculated from it (this is the wrapper in which the slides lie)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question