修复 XL 屏幕上的 Bootstrap 4 个多项目轮播

Fixing a Bootstrap 4 multiple item carousel on XL screen

(这绝对不是重复的)。

我实现了一个 Bootstrap 4 显示多个项目的轮播。它必须满足不同屏幕尺寸的这些特定要求:

xl 媒体查询外,它工作得很好。我已经尝试解决此问题数小时但未成功:

  1. 如果屏幕宽度为 1200 像素或更大 (xl),轮播仅显示 4 或 5 个项目而不是 6 个,因此在循环显示项目时会出现奇怪的闪烁行为。

sm 屏幕 675px。这很好用。

md 屏幕 768px。这很好用。

lg 屏幕 992pxx。这很好用。

xl screen 1200px 如您所见,只显示了 4 个项目(开始示例时有时显示 5 个)。此屏幕尺寸应显示 6 个项目。

  1. 如果您按 Prev 然后按 Next,从右边进入的新项目会有一个小的但无法识别的延迟轮播。

必须修复第一个错误,但最好也修复第二个错误。

我没有在此处粘贴代码,而是在下面的 link 中添加了一个实例:

https://jsitor.com/SQENGSYDz

感谢您的帮助!

这是您的解决方案,基于此

如果您发现幻灯片有问题,可以使用 概念(我自己的问题)

$('#recipeCarousel').carousel({
    interval: 250000
})

$('.carousel .carousel-item').each(function(){
    var minPerSlide = 4;
    var next = $(this).next();
    if (!next.length) {
        next = $(this).siblings(':first');
    }
    next.children(':first-child').clone().appendTo($(this));

    for (var i=0;i<minPerSlide;i++) {
        next=next.next();
        if (!next.length) {
            next = $(this).siblings(':first');
        }

        next.children(':first-child').clone().appendTo($(this));
    }
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

<style>
.carousel-inner .carousel-item.active,
.carousel-inner .carousel-item-next,
.carousel-inner .carousel-item-prev {
    display: flex;
}

.carousel-inner .carousel-item-right.active,
.carousel-inner .carousel-item-next {
    transform: translateX(50%);
}

.carousel-inner .carousel-item-left.active, 
.carousel-inner .carousel-item-prev {
    transform: translateX(-50%);
}

@media (min-width: 768px) and (max-width: 992px) {
    .carousel-inner .carousel-item-right.active,
    .carousel-inner .carousel-item-next {
        transform: translateX(33.33%);
    }

    .carousel-inner .carousel-item-left.active, 
    .carousel-inner .carousel-item-prev {
        transform: translateX(-33.33%);
    }
}

@media (min-width: 992px) and (max-width: 1200px) {
    .carousel-inner .carousel-item-right.active,
    .carousel-inner .carousel-item-next {
        transform: translateX(25%);
    }

    .carousel-inner .carousel-item-left.active, 
    .carousel-inner .carousel-item-prev {
        transform: translateX(-25%);
    }
}

@media (min-width: 1200px) {
    .carousel-inner .carousel-item-right.active,
    .carousel-inner .carousel-item-next {
        transform: translateX(16%);
    }

    .carousel-inner .carousel-item-left.active, 
    .carousel-inner .carousel-item-prev {
        transform: translateX(-16%);
    }
}

.carousel-inner .carousel-item-right,
.carousel-inner .carousel-item-left{ 
    transform: translateX(0);
}
</style>

<div class="container text-center my-3">
  <h2 class="font-weight-light">Bootstrap 4 - Multi Item Carousel</h2>
  <div class="row mx-auto my-auto">
      <div id="recipeCarousel" class="carousel slide w-100" data-ride="carousel">
          <div class="carousel-inner w-100" role="listbox">
              <div class="carousel-item active">
                  <img class="img-fluid col-6 col-md-4 col-lg-3 col-xl-2" src="http://placehold.it/380?text=1">
              </div>
              <div class="carousel-item">
                  <img class="img-fluid col-6 col-md-4 col-lg-3 col-xl-2" src="http://placehold.it/380?text=2">
              </div>
              <div class="carousel-item">
                  <img class="img-fluid col-6 col-md-4 col-lg-3 col-xl-2" src="http://placehold.it/380?text=3">
              </div>
              <div class="carousel-item">
                  <img class="img-fluid col-6 col-md-4 col-lg-3 col-xl-2" src="http://placehold.it/380?text=4">
              </div>
              <div class="carousel-item">
                  <img class="img-fluid col-6 col-md-4 col-lg-3 col-xl-2" src="http://placehold.it/380?text=5">
              </div>
              <div class="carousel-item">
                  <img class="img-fluid col-6 col-md-4 col-lg-3 col-xl-2" src="http://placehold.it/380?text=6">
              </div>
          </div>
          <a class="carousel-control-prev w-auto" href="#recipeCarousel" role="button" data-slide="prev">
              <span class="carousel-control-prev-icon bg-dark border border-dark rounded-circle" aria-hidden="true"></span>
              <span class="sr-only">Previous</span>
          </a>
          <a class="carousel-control-next w-auto" href="#recipeCarousel" role="button" data-slide="next">
              <span class="carousel-control-next-icon bg-dark border border-dark rounded-circle" aria-hidden="true"></span>
              <span class="sr-only">Next</span>
          </a>
      </div>
  </div>
  <h5 class="mt-2">Advances one slide at a time</h5>
</div>

<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>

希望你喜欢我的第二个回答。

说明

the screen's width is 1200px or more (xl), the carousel is only displaying 4 or 5 items instead of 6, therefore causing a strange flickering behavior when cycling through the items.

每第4个元素不能得到transition的CSS,所以我在@media query (min-width: 1200px)中添加了它的CSS,现在它显示6个项目。

As you can see, only 4 items are being displayed (sometimes 5 when starting the example). This screen size should be displaying 6 items.

因为您在代码中添加了 var items_per_slide = Math.round(total_items / 2);,并且 total_items 总是 7,所以它会给您 4 答案,所以这仅适用于一行中的 4 个项目。

所以我在你的代码中静态添加了6,你的问题就解决了。

if you press Prev and then Next, there will be a small but unidentified delay for the new item coming in from the right of the carousel.

这是因为你 hard-coded 33.333333333333333% 和 -16.666666666666667% 在你的代码中造成了一些延迟。

https://codepen.io/nisharg/pen/YzyGvQN?editors=0111