Twitter Bootstrap Carousel - access current index

后端 未结 9 1228
面向向阳花
面向向阳花 2020-12-02 14:11

How do I get the current index from the carousel?

In this case I am using an unordered list. I know I could search through the list items to find the one with the \'

相关标签:
9条回答
  • 2020-12-02 14:59

    I was able to access the slide index using a jquery function from the bootstrap documentation.

    $('#carousel').on('slide.bs.carousel', function(event) {
        alert(event.from);
       // You can use use 'event.from' in whatever way works best for you here
    }
    

    .from on the event will give you the slide where the slide instance occurs. .to will give you the slide where the slide instance is going to. Using slide.bs.carousel will execute the function before the slide animation, slid.bs.carousel will execute the function after the slide animation but the returned slide number will be the same.

    There's a few other properties that the slide and slid events give you access to, I recommend checking them out on the carousel documentation page.

    Note: I am using bootstrap 4.3.1.

    0 讨论(0)
  • 2020-12-02 15:05

    Here's what I came up with after reading fat's comment in @Quelltextfabrik's answer.

    var carousel = $("#myCarousel");
    
    carousel.on("slid", function () {
        var all = carousel.find('.item'),
            active = carousel.find('.active'),
            slidTo = all.index(active);
    
        console.log("Slid - Slid To: " + slidTo);
        });
    
    carousel.on("slide", function () {
        var all = carousel.find('.item'),
            active = carousel.find('.active'),
            slidFrom = all.index(active);
    
        console.log("Slide - Slid From: " + slidFrom);
    });
    
    0 讨论(0)
  • 2020-12-02 15:11

    $(".active", e.target).index() works. Where e from:

    carousel.on("slid", function (e) {
       $(".active", e.target).index();
    });
    

    Found on: https://github.com/twitter/bootstrap/pull/2404#issuecomment-4589229

    0 讨论(0)
提交回复
热议问题