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&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&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&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&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&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&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
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 tocol-xs-12 col-md-4
or something similar and the othercolumn
‘s class tohidden-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 thecol-md-4
will make an item take 33.3..% of it’s row’s width. Thehidden-xs
andhidden-sm
will prevent the items to be displayed onxs
andsm
screens.You change your Markup to:
and remove this CSS:
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
I found a solution. Change
col-xs-4
tocol-md-4
.Then change javascript code to
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
IN DESKTOP
NOTE : Refresh your browser after changing width each times.
Hope it will help