具有多个元素的响应式twitter bootstrap轮播

时间:2016-01-15 07:17:34

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

我有一个Twitter引导旋转木马,连续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));
    }
});

2 个答案:

答案 0 :(得分:-1)

我找到了解决方案。将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

IN DESKTOP

enter image description here

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

希望它会有所帮助

答案 1 :(得分:-2)

请参阅Bootstrap grid system文档,您将找到解决问题所需的规则。

您将第一个column的课程更改为col-xs-12 col-md-4或类似内容,将另一个column的课程更改为hidden-xs hidden-sm col-md-4

col-xs-12会在非常小的屏幕上显示100%的项目,一旦您进入中等屏幕,col-md-4将使项目占据行宽的33.3%。 hidden-xshidden-sm会阻止商品在xssm屏幕上显示。

您将标记更改为:

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

并删除此CSS:

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

您的Javascript也会复制您的元素,但由于您需要在列上使用不同的类,因此您需要在需要时对其进行更改。

修改 我想我只想知道你真正想做什么,抱歉以前误解了。

这是JSFiddle