bootstrap carousel pause not working

2019-08-02 21:04发布

问题:

I'm unable to invoke the 'pause' function on the Bootstrap (3.0.2) carousel. I've tried every way I can think of to refer to the carousel instance, but - no errors, no pause.

<script>
    var stepIndex = 3;
    $('.carousel').carousel({
        interval: 3000
    });
    $('#carousel-couples').on('slide.bs.carousel', function () {
        stepIndex--
        console.log("carousel stepping", stepIndex);
        if (stepIndex == 0) {
            console.log("supposedly pausing carousel");
            $('.carousel').carousel('pause');
        }
    })
</script>
</body>
</html>

回答1:

Calling pause on a carousel while its sliding does not seem to work. Instead use the slid.bs.carousel event runs once the slide has finished the transition and then your pause should work.

$('#carousel-couples').on('slid.bs.carousel', function () {
    ...
});