I am using bootstrap 4.0 version. I want to hide left control on the first item, and hide the right control on the last item. I think using jQuery it can be solved.
Left control will hide all the time it only show on the last item. And right control will show all the time but hide in the last item.
HTML:
<div id="carousel-1" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
Slide 1
</div>
<div class="carousel-item">
Slide 2
</div>
<div class="carousel-item">
Slide 3
</div>
</div>
<a class="carousel-control-prev" href="#carousel-1" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carousel-1" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
JQuery:
$('.carousel').carousel({
interval: false,
})
function checkitem()
{
var $this = $('#carousel-1');
if ($('.carousel-inner .carousel-item:first').hasClass('active')) {
$this.children('.carousel-control-prev').hide();
} else if ($('.carousel-inner .carousel-item:last').hasClass('active')) {
$this.children('.carousel-control-next').hide();
} else {
$this.children('.carousel-control').show();
}
}
I removed the data-ride="carousel"
so it doesn't start automatically, setted interval: false
and wrap: false
.
Added .d-none
(Bootstrap class) to the control
s so they start hidden.
If there are at least two items, then the next
control is displayed.
Then, on every slide we check the next position and show the control
accordingly.
var carouselLength = $('.carousel-item').length - 1;
// If there is more than one item
if (carouselLength) {
$('.carousel-control-next').removeClass('d-none');
}
$('.carousel').carousel({
interval: false,
wrap: false
}).on('slide.bs.carousel', function (e) {
// First one
if (e.to == 0) {
$('.carousel-control-prev').addClass('d-none');
$('.carousel-control-next').removeClass('d-none');
} // Last one
else if (e.to == carouselLength) {
$('.carousel-control-prev').removeClass('d-none');
$('.carousel-control-next').addClass('d-none');
} // The rest
else {
$('.carousel-control-prev').removeClass('d-none');
$('.carousel-control-next').removeClass('d-none');
}
});
.carousel {
background-color: #ddd;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<div id="carousel-1" class="carousel slide">
<div class="carousel-inner">
<div class="carousel-item active">
Slide 1
</div>
<div class="carousel-item">
Slide 2
</div>
<div class="carousel-item">
Slide 3
</div>
</div>
<a class="carousel-control-prev d-none" href="#carousel-1" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next d-none" href="#carousel-1" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With