Reputation: 856
Basically what I am trying to do is, I have one Bootstrap carousel with 8 slides in it.I am using 2 carousel-indicators.
This is my first indicator it work fine with .active
class
<ol class="rightci carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" ></li>
</ol>
this is my Second Indicator it works fine.when I click on the link carousel slides change perfectly but .active
class dose not change automatically rather on first indicators .active
changes fine.
<div class="left leftci carousel-indicators">
<a href=""><span class="leftindicators active" data-target="#carousel-example-generic" data-slide-to="0">slide1</span></a>
</div>
help me with it
Upvotes: 2
Views: 3539
Reputation: 7213
Here is another solution without using JQuery.
I gave the second indicator bar the ID #custom-carousel-indicators.
let carouselIndicatorButtons = document.querySelectorAll('#custom-carousel-indicators button')
document.querySelectorAll('.carousel').forEach((button) => {
button.addEventListener('slid.bs.carousel', function () {
let activeIndicator = document.querySelector('.carousel-indicators button.active')
carouselIndicatorButtons.forEach((button2) => {
button2.classList.remove('active')
})
document.querySelector('#custom-carousel-indicators button[data-bs-slide-to="'
+ activeIndicator.dataset.bsSlideTo + '"]').classList.add('active')
});
});
Upvotes: 0
Reputation: 21
I too faced similar kind of issue. So I too used above given solution. But was not working correctly on click. So here is what I did to fix this issue 100%. With little enhancement on above solution.
Issue: I wanted to show 2 indicators for one carousel. Everything works fine after using above solution but fails after clicking.
Solution: So I tried this
$('.carousel').on('slid.bs.carousel', function() {
var indicatorsAct = $(".carousel-indicator-icons li.active").data("slide-to");
$(".carousel-indicators-numbers li").removeClass("active");
$(".carousel-indicators-numbers").find("[data-slide-to='" + indicatorsAct + "']")
.addClass("active");
});
$('ol.carousel-indicator-icons li').on("click",function(){
$('ol.carousel-indicator-icons li.active').removeClass("active");
$("ol.carousel-indicators-numbers li.active").removeClass("active");
$(this).addClass("active");
var indicators = $(this).data("slide-to");
$(".carousel-indicators-numbers").find("[data-slide-to='" + indicators + "']")
.addClass("active");
});
Where .carousel-indicator-icons
is the container class for first indicator and .carousel-indicators-numbers
is the class for second indicator.
I hope this will help to other folks who faced similar issue.
Upvotes: 2
Reputation: 6837
I think you need to write some custom js Bootstrap has not this functionality by default so you need to make your own carousel-indicators
css
.carousel-indicators2 {
position: absolute;
top: 10px;
left: 50%;
z-index: 15;
width: 60%;
padding-left: 0;
margin-left: -30%;
text-align: center;
list-style: none;
}
.carousel-indicators2 li {
display: inline-block;
width: 10px;
height: 10px;
margin: 1px;
text-indent: -999px;
cursor: pointer;
background-color: #000\9;
background-color: rgba(0, 0, 0, 0);
border: 1px solid #fff;
border-radius: 10px;
}
.carousel-indicators2 .active {
width: 12px;
height: 12px;
margin: 0;
background-color: #fff;
}
JS
$('.carousel').on('slid.bs.carousel', function() {
$(".carousel-indicators2 li").removeClass("active");
indicators = $(".carousel-indicators li.active").data("slide-to");
a = $(".carousel-indicators2").find("[data-slide-to='" + indicators + "']").addClass("active");
console.log(indicators);
})
Upvotes: 2