Update the current number of slide when jumping to specific item - Bootstrap 3 Carousel

前端 未结 2 586
被撕碎了的回忆
被撕碎了的回忆 2020-12-22 08:16

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 upda

2条回答
  •  时光说笑
    2020-12-22 08:58

    Use $('.carousel').carousel(number) to jump to a specific slide.

    From the bootstrap docs

    .carousel(number)

    Cycles the carousel to a particular frame (0 based, similar to an array).

    See this question for more information on jumping to a specific slide using hash values.

    Also, your code to jump to a slide will execute before your custom code to update pager-text is registered, it also wont register as a "slide" because you are not using the built in function to jump to a slide.

    Something like this should give you the results you are looking for.

    $(document).ready(function() {  
        //Initiate carousel
        $('.carousel').carousel({
            interval: false
        })
    
        //Event: Update pager-text on slide
        $('.carousel').on('slid.bs.carousel', function () {
            ...
        });
    
        //Jump to slide on page load
        $('.carousel').carousel(number);
    
    }); 
    

提交回复
热议问题