简体   繁体   English

Bootstrap carousel 如何在 xs 大小的滑块中隐藏一项?

[英]Bootstrap carousel how to hide one item from slider on xs size?

How I can hide one or two items from the bootstrap-carousel only on xs-size ?如何仅在 xs-size 上从 bootstrap-carousel 中隐藏一两个项目?

When I added class 'hidden-xs' to this item in carousel and item div looks like:当我在 carousel 和 item div 中向这个项目添加类“hidden-xs”时,它看起来像:

<div class="item hidden-xs">
      <img src="imgTop2.jpg" alt="...">
</div>

All the items and the carousel disappeared.所有的物品和旋转木马都消失了。

The same problem exist when i add 'hidden-xs' class to img element for this item.当我将 'hidden-xs' 类添加到该项目的 img 元素时,存在同样的问题。

How can I fix that ?我该如何解决? I want to hide on xs only one or two slides.我只想在 xs 上隐藏一两张幻灯片。 Others must by visible.其他人必须通过可见。

My code looks like:我的代码看起来像:

<div id="carousel-top" class="carousel slide" data-ride="carousel">
    <!-- Indicators -->
    <ol class="hidden carousel-indicators" style="display:none">
      <li data-target="#carousel-top" data-slide-to="0" class="active"></li>
      <li class="" data-target="#carousel-top" data-slide-to="1"></li>
      <li class="" data-target="#carousel-top" data-slide-to="2"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">

      <div class="item active">
      <img src="imgTop.jpg" alt="...">
    </div>

    <div class="item">
      <img src="imgTop2.jpg" alt="...">
    </div>

    <div class="item">
      <img src="imgTop3.jpg" alt="...">
    </div>

    <div class="item">
      <img src="imgTop4.jpg" alt="...">
    </div>

    <div class="item">
      <img src="imgTop5.jpg" alt="...">
    </div>


    </div>

    <!-- Controls -->
    <a class="hidden left carousel-control" href="#carousel-top" role="button" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="hidden right carousel-control" href="#carousel-top" role="button" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>

I can't think of a way to do it just with bootstrap helper classes, because the carousel script depends on the .item class.我想不出只使用引导助手类的方法,因为轮播脚本依赖于.item类。 But you could use the following jQuery:但是您可以使用以下 jQuery:

if ($(window).width() < 960) {
     $('#carousel-top .hide-on-mobile').removeClass('item').addClass('hide');
}

You just have to add the class .hide-on-mobile to the items you want to hide on mobile devices.您只需将.hide-on-mobile类添加到要在移动设备上隐藏的项目。

Working Example工作示例

Expanding on Sebsemillia's approach, you can achieve this conditional logic actively on the window resize event.扩展 Sebsemillia 的方法,您可以在窗口调整大小事件上主动实现此条件逻辑。 The only catch is that if the element has class "active", you must also remove this class and add it to another slide without the .hide-on-mobile class.唯一的问题是,如果元素具有“active”类,您还必须删除此类并将其添加到另一个没有.hide-on-mobile类的幻灯片中。 See here, using class .no-mobile --看到这里,使用类.no-mobile --

 $(function(){ var $window = $(window); function deviceWidth() { return $window.width(); } function toggleMobileSlideVisibility(show_hide) { $no_mobile_slides = $('.carousel-inner').find('.no-mobile'); if (show_hide === 'hide'){ var reset_active_slide = false; $no_mobile_slides.each(function(i, e){ if ($(e).hasClass('active')) { reset_active_slide = true; $(e).removeClass('active'); } }); $no_mobile_slides.removeClass('item').addClass('hide'); if (reset_active_slide) { $('.carousel-inner').find('.item').first().addClass('active'); } } else if (show_hide === 'show') { $no_mobile_slides.addClass('item').removeClass('hide'); } } var is_mobile_device = false; var detectMobile = function detectMobile(){ if (deviceWidth() > 978) { if (is_mobile_device) { toggleMobileSlideVisibility('show'); } is_mobile_device = false; } else { if (!is_mobile_device) { toggleMobileSlideVisibility('hide'); } is_mobile_device = true; } } $(window).on('resize', function(){ detectMobile(); }); detectMobile(); });
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <div id="carousel-example-generic" class="carousel slide" data-ride="carousel"> <!-- Indicators --> <ol class="carousel-indicators"> <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li> <li data-target="#carousel-example-generic" data-slide-to="1"></li> <li data-target="#carousel-example-generic" data-slide-to="2"></li> </ol> <!-- Wrapper for slides --> <div class="carousel-inner" role="listbox"> <div class="item active"> <img src="https://unsplash.it/1920/600/?random&img=2" alt="random"> <div class="carousel-caption"> Just A Slide </div> </div> <div class="item no-mobile"> <img src="https://unsplash.it/1920/600/?random&img=3" alt="random-no-mobile"> <div class="carousel-caption"> A Slide That Should Hide On Mobile </div> </div> <div class="item"> <img src="https://unsplash.it/1920/600/?random&img=4" alt="random2"> <div class="carousel-caption"> Just Another Slide </div> </div> </div> <!-- Controls --> <a class="left carousel-control" href="#carousel-example-generic" 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="#carousel-example-generic" role="button" data-slide="next"> <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> <span class="sr-only">Next</span> </a> </div>

Or you can use CSS media query to hide the items you choose on mobile:或者您可以使用 CSS 媒体查询来隐藏您在移动设备上选择的项目:

@media screen and (max-device-width: 800px) {
  .slide-hide-on-mobile { display: none; }
}

You just need to add class="slide-hide-on-mobile" to the items you want to hide:您只需要将 class="slide-hide-on-mobile" 添加到要隐藏的项目中:

<div class="item active slide-hide-on-mobile">
  <img src="imgTop.jpg" alt="...">
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM