在移动设备中显示一个Bootstrap轮播项目 [英] Show one Bootstrap carousel item in mobile

查看:76
本文介绍了在移动设备中显示一个Bootstrap轮播项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Bootstrap Carousel,它在Desktop视图中显示了2个项目。但是,我想在Mobile视图中一次显示一个项目。我怎样才能做到这一点?



解决方案

  $(document).ready(function(){
$(#myCarousel)。carousel();
if($(window) .width()< 640){
$('。col-sm-6')。unwrap()。addClass('item');
$('。col-sm-6:第一个')。addClass('active');
}
});

其中640是您的移动断点。查看 unwrap(),它会删除元素的父级,同时不影响其内容。


I have this Bootstrap Carousel which shows 2 item each in Desktop view. However, I want to show one item at a time in Mobile view. How can I achieve this?

https://jsfiddle.net/v9t2pz9b/

HTML

<div class="container">
  <div id="myCarousel" class="carousel slide" data-ride="carousel">
    <div class="carousel-inner" role="listbox">
      <div class="item active">
        <div class="col-sm-6">
          block1
        </div>
        <div class="col-sm-6">
          block2
        </div>
      </div>
      <div class="item">
        <div class="col-sm-6">
          block3
        </div>
        <div class="col-sm-6">
          block4
        </div>
      </div>

      <div class="item">
        <div class="col-sm-6">
          block5
        </div>
        <div class="col-sm-6">
          block6
        </div>
      </div>

    </div>

    <!-- Left and right 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>
</div>

JS:

$(document).ready(function(){
     $("#myCarousel").carousel();
});

Something like as this shown in the design. I want to show only one slide at a time in mobile:

解决方案

$(document).ready(function(){
  $("#myCarousel").carousel();
    if ( $(window).width() < 640 ) {
       $('.col-sm-6').unwrap().addClass('item');
       $('.col-sm-6:first').addClass('active');
    }
});

Where 640 is your mobile breakpoint. Check out unwrap() , it removes the element's parent while not affecting its content.

这篇关于在移动设备中显示一个Bootstrap轮播项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆