html - 具有多个元素的响应式 Twitter Bootstrap 轮播

标签 html css twitter-bootstrap twitter-bootstrap-3 responsive-design

我有一个 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&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>

.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 张图像。

Link to my fiddle

在移动设备中

enter image description here

在桌面上

enter image description here

注意:每次更改宽度后请刷新浏览器。

希望对你有帮助

关于html - 具有多个元素的响应式 Twitter Bootstrap 轮播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34806040/

相关文章:

javascript - 显示 javascript 结果,这是输入标签内日期之间差异的结果

javascript - 如何剪掉 viewBox 之外的内容?

html - 我可以设置溢出: hidden but still show a scrollbar?吗

javascript - 将 CSS 样式的 HTML 导出为 PDF

css - 推特 Bootstrap 3 : Nav Should Take up Remainder of Screen Width

javascript - 如何使用 jquery 或 javascript 禁用带有提交类型的按钮标签?

html - 如何取消html文件上传?

javascript - 网格内的网格 Javascript 和 JQuery :

html - 导航栏上的 Bootstrap 断点

css - 如何在导航选项卡 Bootstrap 用户界面(angularjs)中将文本对齐到顶部?