Bootstrap carousel how to hide one element from the slider by size xs?

How can I hide one or two elements from the bootstrap carousel only in xs size?

When I added the "hidden-xs" class to this element in the carousel, and the div element looks like this:

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

All items and the carousel have disappeared.

The same problem exists when I add the 'hidden-xs' class to the img element for this element.

How can i fix this? I want to hide on xs only one or two slides. Others must be visible.

My code looks like this:

 <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> 
+5
source share
3 answers

I can't think of a way to do this only with the help of bootstrap helper classes, because the script carousel depends on the .item class. But you can use the following jQuery:

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

You just need to add the .hide-on-mobile class to the elements you want to hide on mobile devices.

Working example

+5
source

Extending Sebsemillia's approach, you can actively use this conditional logic in a window resize event. The only catch is that if the element has an "active" class, you must also remove this class and add it to another slide without the .hide-on-mobile class. See here using the .no-mobile class -

 $(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> 
0
source

Or you can use the CSS multimedia query to hide the elements that you selected on the mobile device:

 @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 elements you want to hide:

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

All Articles