I guess I'm fighting a bug. I build a small carousel in jquery. It's more some boxs stacked on each other, that will change their z-index (jquery.css) when my user clicks the next and prev arrows. So far so good, the carousel is connected with owl-carousel, but it's independent. Each box has a class that I store in an array and I loop through with jquery. The problem is the following:
Jquery:
$(document).ready(function () {
var carousel = $('.covercarousel');
var carouselnum = carousel.length;
var i = 0;
$('.owl-next').click(function () {
i++;
$(carousel).css('zIndex', '0');
$(carousel[i]).css('zIndex', '2');
if (i == (carouselnum-1)) {
$(carousel).css('zIndex', '2');
i = -1;
}
});
$('.owl-prev').click(function () {
i--;
$(carousel).css('zIndex', '0');
$(carousel[i]).css('zIndex', '2');
if (i < 0) {
i = carouselnum-1;
$(carousel[5]).css('z-index', '2');
$(carousel).css('zIndex', '0');
}
});
});
HTML:
<div class="covercarouselbox">
<div class="covercarousel">
<div>
a
</div>
</div>
<div class="covercarousel">
<div>
b
</div>
</div>
<div class="covercarousel">
<div>
c
</div>
</div>
<div class="covercarousel">
<div>
d
</div>
</div>
<div class="covercarousel">
<div>
e
</div>
</div>
<div class="covercarousel">
<div>
f
</div>
</div>
</div>
The carousel works fine when you scroll to the next div. If you console log the (i), you can notice that the count works fine from 0 to 4 (there are 6 elements in the array, I gave to the first one a z-index 2 with css => covercarousel:nth-child(1) {z-index=2} ), so when you reach the fifth element in the array, the value assigned to the i is -1, so the count comes back to 0 (i++).
But when you do the opposite it brakes: The first value is 0, so when you add the -1 to the array pressing the prev arrow, and then you try to scroll to the next slide inside adding a +1 to the i for some reason, if you console log the value of the (i), the result will be 5 and then it will keep growing. Is there any way to prevent this behavior?