Bootstrap 4 Multi Carousel显示4个图像而不是3个

时间:2018-02-05 21:00:49

标签: carousel bootstrap-4

我无法弄清楚如何让它显示4张图片而不是3张。

Based on this Code: Bootstrap 4 Multiple Item Carousel

脚本JS

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

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

    if (next.next().length>0) {
    next.next().children(':first-child').clone().appendTo($(this));
    }
    else {
      $(this).siblings(':first').children(':first-child').clone().appendTo($(this));
    }
});

CSS

.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%)
}

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

我已更改为此代码

<div class="carousel-item col-md-3">

而不是原始代码

<div class="carousel-item col-md-4">

1 个答案:

答案 0 :(得分:1)

由于4个cols,25%宽度(而不是33%)更改CSS:

.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%)
}

还需要将其他项目克隆到每张幻灯片中。所以JS的变化是:

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

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

        next.children(':first-child').clone().appendTo($(this));
      }
});

Bootstrap 4.0.0 Demo - 4 slides, advance 1

注意:从alpha 6(当询问原始问题时)到 Bootstrap 4.0.0 时,活动轮播项目更改为flexbox。因此,必须对多轮播中的相邻项目进行相同的操作。

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

另见:Bootstrap 4.1.0 (advance all 4 at once)