Reputation: 6316
Can you please let me know how I can get the total and current number of the carousel slides in bootstrap like the image below?
I have an standard Bootstrap carousel and a <div>
with the .num
class to display the total and current number and I used this code to retrieve the numbers but it didn't go through
$('.num').html(){
$('#myCarousel').carousel({number})
}
Thanks
Update:
Please find a sample at this jsfiddle LINK
Upvotes: 37
Views: 117957
Reputation: 528
in bootstrap 5 with angular
in html file
<div id="carouselExampleIndicators" class="carousel slide" data-bs-ride="carousel">
<div class="carousel-indicators">
<button type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button>
<button type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide-to="1" aria-label="Slide 2"></button>
<button type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide-to="2" aria-label="Slide 3"></button>
</div>
<div class="carousel-inner">
<div class="carousel-item active">
<img src="..." class="d-block w-100" alt="...">
</div>
<div class="carousel-item">
<img src="..." class="d-block w-100" alt="...">
</div>
<div class="carousel-item">
<img src="..." class="d-block w-100" alt="...">
</div>
</div>
<button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="visually-hidden">Previous</span>
</button>
<button class="carousel-control-next" type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="visually-hidden">Next</span>
</button>
</div>
<div class="num"></div><!-- this last line used for showing slide number -->
in css file
/** slider css start **/
.carousel-control-prev {
margin-left: 80%;
}
.carousel-control-prev, .carousel-control-next {
margin-top: -42px;
bottom: unset;
}
.carousel-control-prev-icon {
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23'%3e%3cpath d='M11.354 1.646a.5.5 0 0 1 0 .708L5.707 8l5.647 5.646a.5.5 0 0 1-.708.708l-6-6a.5.5 0 0 1 0-.708l6-6a.5.5 0 0 1 .708 0z'/%3e%3c/svg%3e") !important;
width: 15px;
height: 15px;
}
.carousel-control-next-icon {
background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23'%3e%3cpath d='M4.646 1.646a.5.5 0 0 1 .708 0l6 6a.5.5 0 0 1 0 .708l-6 6a.5.5 0 0 1-.708-.708L10.293 8 4.646 2.354a.5.5 0 0 1 0-.708z'/%3e%3c/svg%3e") !important;
width: 15px;
height: 15px;
}
.carousel-indicators {
margin-bottom: 0px;
}
.carousel-indicators [data-bs-target] {
background-color: #cbb06d;
}
.num{
background:#000;
color:#fff;
padding:10px;
width:50px;
text-align:center;
}
/** slider css end **/
in ts file
/** getting slide number start */
var totalItems = $('.carousel-item').length;
var currentIndex = $('div.active').index() + 1;
$('.num').html('' + currentIndex + '/' + totalItems + '');
$('#carouselExampleCaptions').carousel({
interval: 2000
});
$('#carouselExampleCaptions').bind('slid.bs.carousel', function() {
currentIndex = $('div.active').index() + 1;
$('.num').html('' + currentIndex + '/' + totalItems + '');
});
/** getting silde number end */
Upvotes: 1
Reputation: 9646
Each slide
has a .item
class to it, you can get the total number of slides like this
var totalItems = $('.item').length;
Active slide
has a class named as active
, you can get the index of active slide
like this
var currentIndex = $('div.active').index() + 1;
You can update these values by binding the bootstrap carousel slid
event like this
$('#myCarousel').bind('slid', function() {
currentIndex = $('div.active').index() + 1;
$('.num').html(''+currentIndex+'/'+totalItems+'');
});
Upvotes: 85
Reputation: 140
var totalItemsPop = $('#Mycarousel .item').length;
$('#Mycarousel').on('slide.bs.carousel', function() {
setTimeout(function(){
currentIndexPop = $('#Mycarousel div.active').index() + 1;
$('.num').html('' + currentIndexPop + '/' + totalItemsPop + '');
}, 1000);
});
after slide event, div will be active and can not get active index, so keep your code inside set time out function
Upvotes: 6
Reputation: 69
Bootstrap 4 and js
var totalItems = $('.item').length;
var currentIndex = $('div.item.active').index() + 1;
var down_index;
$('.num').html(''+currentIndex+'/'+totalItems+'');
$(".next").click(function(){
currentIndex_active = $('div.item.active').index() + 2;
if (totalItems >= currentIndex_active)
{
down_index= $('div.item.active').index() + 2;
$('.num').html(''+currentIndex_active+'/'+totalItems+'');
}
});
$(".prev").click(function(){
down_index=down_index-1;
if (down_index >= 1 )
{
$('.num').html(''+down_index+'/'+totalItems+'');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<div class="num"></div>
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" data-wrap="false" data-interval="false">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item item active">
<img class="d-block w-100" src="https://via.placeholder.com/500x300" alt="First slide">
</div>
<div class="carousel-item item">
<img class="d-block w-100" src="https://via.placeholder.com/500x300" alt="Second slide">
</div>
<div class="carousel-item item">
<img class="d-block w-100" src="https://via.placeholder.com/500x300" alt="Third slide">
</div>
</div>
<a class="carousel-control-prev prev" href="#carouselExampleIndicators" 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 next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
Upvotes: 1
Reputation: 16513
The only drawback with the below code will be that it doesn't show up till you go to next slide (triggers after each slide change).
JS/jQuery
$('#carousel-slide').on('slid.bs.carousel', function () {
var carouselData = $(this).data('bs.carousel');
var currentIndex = carouselData.getActiveIndex();
var total = carouselData.$items.length;
var text = (currentIndex + 1) + " of " + total;
$('#carousel-index').text(text);
});
HTML
<div id="carousel-index">
<!-- Number will be added through JS over here -->
</div>
Upvotes: 1
Reputation: 216
You can use jquery index() function to get the current index of active element inside list of item. So the code is look like this:
var currentItem = $("#carousel-1 .item.active" );
var currentIndex = $('#carousel-1 .item').index(currentItem) + 1;
Upvotes: 0
Reputation: 1126
Update of @Khawer Zeshan's code
For Bootstrap 3.0+ use slid.bs.carousel
instead of slid
and on
instead of bind
. So the update code will be like that
var totalItems = $('.item').length;
var currentIndex = $('div.active').index() + 1;
$('#myCarousel').on('slid.bs.carousel', function() {
currentIndex = $('div.active').index() + 1;
$('.num').html(''+currentIndex+'/'+totalItems+'');
});
Upvotes: 18
Reputation: 47
Bootstrap 3.2+:
carouselData.getItemIndex(carouselData.$element.find('.item.active'))
Upvotes: 4