G
G
Gidroce2021-07-19 13:15:32
css
Gidroce, 2021-07-19 13:15:32

How to make multiple sliders work?

Greetings! There is such slider js code https://codepen.io/i-did/pen/yLYRvVL There should be several such sliders on the page, how can I duplicate them so that they work fine?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2021-07-19
@Gidroce

Isolate all code in some context. Next, pass the slider container to this context. And in the code itself, search for control elements not globally in the document, but only within the passed container.
Something like this:

function makeSlider(slider) {
  ...
}

document.querySelectorAll('.slider').forEach(makeSlider)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question