I have a series of div elements that I want to show and hide sequentially using toggleClass. My code works like a charm.
I would now like to go back to the first div when the last div is active, and so on infinitely. I can't figure out how to do this.
var timeout = 2000
setTimeout(function() {
$(".text-sequence .text").each(function(i, el) {
var $this = $(this);
setTimeout(function() {
$this.toggleClass('active')
$(".text-sequence .text").not($this).removeClass('active')
}, i * timeout)
})
}, timeout)
.text {
opacity: 0
}
.text.active {
opacity: 1
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="text-sequence">
<div class="text">Text 1</div>
<div class="text">Text 2</div>
<div class="text">Text 3</div>
<div class="text">Text 4</div>
<div class="text">Text 5</div>
<div class="text">Text 6</div>
</div>