简体   繁体   中英

Bootstrap Carousel: How to stop auto sliding on large screen

I am trying to find a way to make my slide/carousel to stop when the screen reaches 992px or plus (but it is also nice if it back to work again if I manually reduce my screen size even if the user normally doesn't do it).

So, I tried the code below but it is not working, and doing many researches I just find solutions that disable the auto slide from every size of screen.

Do you have any suggestion?

function(){

    var windowIsLarge = window.matchMedia("(min-width:992px)").matches;

    if (windowIsLarge) {
        //carousel disabled
        $('.carousel').carousel({
            interval: false;
        });
    };
};

Heres's my updated code, which doesn't work either. I can't see what is wrong with my code there.

 $(document).ready(updateCarousel);

 $(window).resize(updateCarousel);


function updateCarousel() {
    var $containerWidth = $(document).width();
    if ($containerWidth <= 998) {
        $('.carousel').carousel({
            interval: 500
        });
    }
    if ($containerWidth > 998) {
        $('.carousel').carousel({
            interval: false
        });
    }
}

I found this answer which I think relates, try:

$(window).on('resize', function(){
      var win = $(this);
      if (win.width() > 992) { 

      $('carousel').removeClass('carousel');

      }

});

I think you're sort of on the right track, but you need to add an event listener to your matchMedia so that it updates as the window resizes.

From: https://www.redweb.com/agency/blog/2013/september/responsive-performance-matchmedia

var media = window.matchMedia('(min-width:992px)');
media.addListener(function (mediaQueryList) {
  if (mediaQueryList.matches) {
    $('.carousel').carousel({
      interval: false
    });
  } else {
    $('.carousel').carousel({
      interval: 500
    });
  }
});

update:

  if( $(window).width() > 992 ) {
    // disable carousel
    $('.carousel').carousel({
      interval: false,
    });
  }

use

datainterval = "false" in div tag

<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" data-interval="false" >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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