skip to Main Content

I have a twitter bootstrap carousel with 3 items on a row. The example is here. How I can make it responsive? I want to display one single element for the mobile devices.

<div id="myCarousel" class="carousel slide">  
    <div class="carousel-inner">
        <div class="item active">
            <div class="col-xs-4">
                <a href="#"><img src="http://placehold.it/500/bbbbbb/fff&amp;text=1" class="img-responsive"></a>
            </div>
        </div>
        <div class="item">
             <div class="col-xs-4">
                  <a href="#"><img src="http://placehold.it/500/CCCCCC&amp;text=2" class="img-responsive"></a>
             </div>
        </div>
        <div class="item">
            <div class="col-xs-4">
                <a href="#"><img src="http://placehold.it/500/eeeeee&amp;text=3" class="img-responsive"></a>
            </div>
        </div>
        <div class="item">
            <div class="col-xs-4">
                <a href="#"><img src="http://placehold.it/500/f4f4f4&amp;text=4" class="img-responsive"></a>
            </div>
        </div>
        <div class="item">
            <div class="col-xs-4">
                <a href="#"><img src="http://placehold.it/500/fcfcfc/333&amp;text=5" class="img-responsive"></a>
            </div>
        </div>
        <div class="item">
            <div class="col-xs-4">
                <a href="#"><img src="http://placehold.it/500/f477f4/fff&amp;text=6" class="img-responsive"></a>
            </div>
        </div>
    </div>

    <!-- Controls -->
    <a 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 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>

And

.carousel-inner .active.left  { left: -33%;             }
.carousel-inner .active.right { left: 33%;              }
.carousel-inner .next         { left: 33%               }
.carousel-inner .prev         { left: -33%              }
.carousel-control.left        { background-image: none; }
.carousel-control.right       { background-image: none; }
.carousel-inner .item         { background: white;      }

And

$('#myCarousel').carousel({
    interval: 10000
})

$('.carousel .item').each(function(){
    var next = $(this).next();
    if (!next.length) {
        next = $(this).siblings(':first');
    }
    next.children(':first-child').clone().appendTo($(this));

    if (next.next().length>0) {
        next.next().children(':first-child').clone().appendTo($(this));
    }
    else {
        $(this).siblings(':first').children(':first-child').clone().appendTo($(this));
    }
});

2

Answers


  1. See the Bootstrap grid system documentation, there you will find the rules you need to solve your problem.

    You change the first column‘s class to col-xs-12 col-md-4 or something similar and the other column‘s class to hidden-xs hidden-sm col-md-4.

    The col-xs-12 will display items with 100% on a very small screen and as soon you enter a medium screen the col-md-4 will make an item take 33.3..% of it’s row’s width. The hidden-xs and hidden-sm will prevent the items to be displayed on xs and sm screens.

    You change your Markup to:

    <div class="item active">
          <div class="row">
            <div class="col-xs-12 col-md-4"><a href="#"><img src="http://placehold.it/500/bbbbbb/fff&amp;text=1" class="img-responsive"></a></div>
            <div class="hidden-xs hidden-sm col-md-4"><a href="#"><img src="http://placehold.it/500/bbbbbb/fff&amp;text=1" class="img-responsive"></a></div>
            <div class="hidden-xs hidden-sm col-md-4"><a href="#"><img src="http://placehold.it/500/bbbbbb/fff&amp;text=1" class="img-responsive"></a></div>
          </div>    
        </div>
    

    and remove this CSS:

    .carousel-inner .active.left  { left: -33%;             }
    .carousel-inner .active.right { left: 33%;              }
    .carousel-inner .next         { left: 33%               }
    .carousel-inner .prev         { left: -33%              }
    

    also your Javascript copies your elements, but as you need different classes on the columns you’ll need to alter it if you need it.

    EDIT:
    Think i just figured out what you actually want to do, sorry about misinterpreting before.

    Here’s the JSFiddle

    Login or Signup to reply.
  2. I found a solution. Change col-xs-4 to col-md-4.

    Then change javascript code to

    $('#myCarousel').carousel({
      interval: 10000
    })
    if($( window ).width() >= 768){
    $('.carousel .item').each(function(){
      var next = $(this).next();
      if (!next.length) {
        next = $(this).siblings(':first');
      }
      next.children(':first-child').clone().appendTo($(this));
    
      if (next.next().length>0) {
        next.next().children(':first-child').clone().appendTo($(this));
      }
      else {
        $(this).siblings(':first').children(':first-child').clone().appendTo($(this));
      }
    });
    }
    

    When the browser width is greater than or equal to 768px, then it will show three images in a row. When width is less than 768px, then only 1 image is shown.

    Link to my fiddle

    IN MOBILE DEVICES

    enter image description here

    IN DESKTOP

    enter image description here

    NOTE : Refresh your browser after changing width each times.

    Hope it will help

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search