I modified the standard Boostrap 3 Carousel to be able to jump to a specific slide, when the url matches #
. It works, but my pager-text
is not updated, when jumping to a specific slide. The function for updating the pager-text
is only working after an item has slid. Anyone have a solution?
My html:
<li class="pager-text">1/{{ object.photo_set.count }}</li>
My .js:
$(document).ready(function() {
//Initiate carousel
$('.carousel').carousel({
interval: false
})
$('.carousel').on('slid.bs.carousel', function () {
var carouselData = $(this).data('bs.carousel');
var currentIndex = carouselData.getActiveIndex();
var total = carouselData.$items.length;
// Display the current number of the slide
var text = (currentIndex + 1) + "/" + total;
$('.pager-text').text(text);
// Update location based on slide (index is 0-based)
window.location.hash = "#"+ parseInt($('.carousel .carousel-inner .item.active').index()+1);
});
});
var url = document.location.toString();
if (url.match('#')) {
// Clear active item
$('.carousel .carousel-inner .item.active').removeClass('active');
// Activate item number #hash
$('.carousel-inner div:nth-child(' + url.split('#')[1] + ')').addClass('active');
}