我有一个 Twitter bootstrap 轮播,其中有 3 个元素排成一行。例子是 here .我怎样才能让它响应?我想为移动设备显示一个元素。
<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>
和
.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; }
和
$('#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));
}
});
最佳答案
我找到了一个解决方案。将 col-xs-4
更改为 col-md-4
。
然后将javascript代码改成
$('#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));
}
});
}
当浏览器宽度大于或等于768px时,会连续显示三张图片。当宽度小于 768px 时,则只显示 1 张图像。
在移动设备中
在桌面上
注意:每次更改宽度后请刷新浏览器。
希望对你有帮助
关于html - 具有多个元素的响应式 Twitter Bootstrap 轮播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34806040/