Bootstrap Carousel Next/Prev not working

喜夏-厌秋 提交于 2019-12-24 02:42:47

问题


I am working with Twitter Bootstrap Carousel and though I am not very familiar with .js, somehow I got things working fine.

The only problem is that the next/prev button is working in a strange way.

Here's my temp site: http://tokyo-flaneur.com/dl/donner/contents/menu.html

Although I have 10 images, I can not get to the No.10 by clicking the next button. After the third image, when I click on the next button, the image goes back to No.2.

Also, whichever image I am looking, when I click the prev button, it goes all the way back to No.1.

Could anyone help me or direct me to the right direction? Any help would be appreciated.


回答1:


You handle slider's next/prev buttons with your own handler, so it slide to 2nd element 1st, than tries to make next()

replace:

$("#myCarousel a").click(function(e){ 
    var index = $(this).index(); 
    //when hitting NEXT button index always 2, for PREV - 0
    slider.carousel(index); 
    e.preventDefault(); 
}); 

with

$("#myCarousel .slider-pager a").click(function(e){ 
    var index = $(this).index(); 
    slider.carousel(index); 
    e.preventDefault(); 
}); 

in your onpage script




回答2:


For future visitors to this question:

Use attribute data-interval="false" (Bootstrap 3.3.5):

<div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="false">

Here are other supported carousel options in Bootstrap 3:

http://getbootstrap.com/javascript/#carousel-usage


Earlier versions:

jsFiddle Example

<div id="myCarousel" class="carousel slide text-center" data-ride="carousel">
    <ol class="carousel-indicators">
        <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
        <li data-target="#myCarousel" data-slide-to="1"></li>
    </ol>
    <div class="carousel-inner" role="listbox">
        <div class="item active">
            <h4>A mind-bending lecture on applied philosophy by Oxford lecturer Ravi Zacharias.</h4>
            <br>
            <span class="font-normal">youtube/watch?v=3ZZaoavCsYE</span>
        </div>
        <div class="item">
            <h4>Director of the Human Genome Project discusses the greatest mystery of life</h4>
            <br>
            <span class="font-normal">youtube/watch?v=EGu_VtbpWhE</span>
        </div>
    </div>

    <a href="" class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
    </a>
    <a href="" class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
</div><!-- #myCarousel -->

<script>
    $(function(){

        $('.carousel-control').click(function(e){
            e.preventDefault();
            $('#myCarousel').carousel( $(this).data() );
        });

    });//END document.ready
</script>

Note: this answer requires jQuery, so ensure that library is loaded. Of course, bootstrap requires jQuery so you should already have it referenced.



来源:https://stackoverflow.com/questions/14450328/bootstrap-carousel-next-prev-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!