var elements = document.querySelectorAll('.sliders'); var counter = 1; setInterval(function(){ for (var i = 0; i < elements.length; ++i) { elements[i].style.display = 'none'; } if (counter > elements.length - 1) counter = 0; elements[counter].style.display = 'block'; counter++; }, 1000);
.slider1 { display: block } .slider2 { display: none } .slider3 { display: none }
<div class="slider1 sliders">ΠΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ Π±Π»ΠΎΠΊΠ° 1</div> <div class="slider2 sliders">ΠΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ Π±Π»ΠΎΠΊΠ° 2</div> <div class="slider3 sliders">ΠΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ Π±Π»ΠΎΠΊΠ° 3</div>
setInterval has the syntax setTimeout(func / code, delay[, arg1, arg2...]) , where
func/code - Function or line of code to execute.
delay - The delay in milliseconds, 1000 milliseconds is 1 second.
arg1, arg2β¦ - Arguments to pass to the function.
Here we find all the elements with which to manipulate.
- At each iteration of the counter, we hide all the blocks and display only one specific one.
- Increase the counter.
- Repeat the procedure again (or rather, do it the timer).
- If the counter exceeds the number of items, reset it.