Skip to content Skip to sidebar Skip to footer

Bootstrap Carousel Next/Prev Not Working

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 workin

Solution 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


Solution 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.


Post a Comment for "Bootstrap Carousel Next/Prev Not Working"