具有多列幻灯片的Bootstrap 4 Carousel

时间:2019-01-18 17:03:02

标签: bootstrap-4 carousel

我需要轮播运行时没有图像,而是旋转具有多列的行(col-4)。

它基本上是有效的,但是cols是垂直显示的,而不是代码提示并排显示的3。我想念什么?

 <div id="demo" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="row rss_ticker carousel-item m-0">
      <div class="col-xl-4 col-lg-4 col-md-4 col-sm-4 col-xs-4">

我的代码可以在这里看到。 https://jsfiddle.net/pwf1dahr/1/

1 个答案:

答案 0 :(得分:0)

要完全控制Bootstrap框架中的轮播响应列,并控制每个断点可滑动多少列,请使用Ken Wheelers Slick滑块。这很棒!我的演示只是在摸索,您也可以在每个断点更改滑块选项。见他的documentation

请注意,在我的Slick jQuery javascript中,我定义了mobileFirst: true,这使Slick的断点像Bootstrap的断点一样工作。移动优先!

jQuery

这是一个JavaScript数组,用于复制Bootstrap 4的网格断点,然后将其传递给Slick滑块jQuery函数。下面的代码过于刻板,无法演示如何更改每个Slick幻灯片动画上的列大小以及要滑动的列数。

var breakpoint = {
  // Small screen / phone
  sm: 576,
  // Medium screen / tablet
  md: 768,
  // Large screen / desktop
  lg: 992,
  // Extra large screen / wide desktop
  xl: 1200
};

// page slider
$('#slick').slick({
  autoplay: true,
  autoplaySpeed: 2000,
  draggable: true,
  infinite: true,
  dots: false,
  arrows: false,
  speed: 1000,
  mobileFirst: true,
  slidesToShow: 1,
  slidesToScroll: 1,
  responsive: [
    {
      breakpoint: breakpoint.sm,
      settings: {
        slidesToShow: 2,
        slidesToScroll: 2
      }
    },
    {
      breakpoint: breakpoint.md,
      settings: {
        slidesToShow: 3,
        slidesToScroll: 3
      }
    },
    {
      breakpoint: breakpoint.lg,
      settings: {
        slidesToShow: 4,
        slidesToScroll: 4
      }
    },
    {
      breakpoint: breakpoint.xl,
      settings: {
        slidesToShow: 5,
        slidesToScroll: 5
      }
    }
  ]
});

Sass(css)

如果您不使用sass,请让我知道我将使用CSS更新。下面的这个sass / css模仿了Bootstrap 4的行/列边距和填充样式。

#slick {

  .slick-list {
    margin: {
      left: -15px;
      right: -15px;
    }
  }

  .slick-slide {
    padding-right: 15px;
    padding-left: 15px;

    &:focus {
      outline: none;
    }
  }
}

非常简单,而且控制方式比Bootstrap的轮播提供。

请参见此jsfiddle,您可以在其中测试Bootstrap的每个断点,并在每个断点上看到上面我的脚本中定义的更改。上面的jQuery Slick函数,当Slick滑动到下一张幻灯片时,我已经在每个断点slidesToScroll上设置了相同数量的slidesToShow ..但您可以将这些设置更改为所需的设置

此方法的好处是,您可以将所有文章/帖子循环到一个容器中,而不用中断循环以输出额外的.row div。只需使用Slick的功能来控制每个断点显示多少列。

查看工作演示https://jsfiddle.net/joshmoto/rh1wymuk/

请参见下面的stackoverflow演示,但您将无法测试断点

var breakpoint = {
  // Small screen / phone
  sm: 576,
  // Medium screen / tablet
  md: 768,
  // Large screen / desktop
  lg: 992,
  // Extra large screen / wide desktop
  xl: 1200
};

// slick slider
$('#slick').slick({
  autoplay: true,
  autoplaySpeed: 2000,
  draggable: true,
  infinite: true,
  dots: false,
  arrows: false,
  speed: 1000,
  mobileFirst: true,
  slidesToShow: 1,
  slidesToScroll: 1,
  responsive: [{
      breakpoint: breakpoint.sm,
      settings: {
        slidesToShow: 2,
        slidesToScroll: 2
      }
    },
    {
      breakpoint: breakpoint.md,
      settings: {
        slidesToShow: 3,
        slidesToScroll: 3
      }
    },
    {
      breakpoint: breakpoint.lg,
      settings: {
        slidesToShow: 4,
        slidesToScroll: 4
      }
    },
    {
      breakpoint: breakpoint.xl,
      settings: {
        slidesToShow: 5,
        slidesToScroll: 5
      }
    }
  ]
});
#slick .slick-list {
  margin-left: -15px;
  margin-right: -15px;
}

#slick .slick-slide {
  padding-right: 15px;
  padding-left: 15px;
}

#slick .slick-slide:focus {
  outline: none;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<!-- Slick CSS -->
<link href="https://cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.css" rel="stylesheet" />
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<!-- Slick jQuery min -->
<script src="https://cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.min.js"></script>

<header>
  <nav class="navbar navbar-expand-md navbar-dark bg-dark">

    <a class="navbar-brand mr-auto" href="#">Slick in Bootstrap 4</a>

    <a class="nav-link d-none d-sm-inline" href="http://kenwheeler.github.io/slick/" target="_blank">Slick Github</a>

  </nav>
</header>

<main class="pt-3 pb-3">
  <div class="container">
    <div id="slick">

      <div class="slide">
        <div class="card">
          <img src="https://via.placeholder.com/600x400&amp;text=thumbnail" class="card-img-top" />
          <div class="card-body">
            <h5 class="card-title">Article title</h5>
            <p class="card-text">Ut sed ligula vel felis vulputate lobortis id eget mauris. Nullam sollicitudin arcu ac diam ornare, eget iaculis nisl accumsan.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
      </div>

      <div class="slide">
        <div class="card">
          <img src="https://via.placeholder.com/600x400&amp;text=thumbnail" class="card-img-top" />
          <div class="card-body">
            <h5 class="card-title">Article title</h5>
            <p class="card-text">Ut sed ligula vel felis vulputate lobortis id eget mauris. Nullam sollicitudin arcu ac diam ornare, eget iaculis nisl accumsan.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
      </div>

      <div class="slide">
        <div class="card">
          <img src="https://via.placeholder.com/600x400&amp;text=thumbnail" class="card-img-top" />
          <div class="card-body">
            <h5 class="card-title">Article title</h5>
            <p class="card-text">Ut sed ligula vel felis vulputate lobortis id eget mauris. Nullam sollicitudin arcu ac diam ornare, eget iaculis nisl accumsan.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
      </div>

      <div class="slide">
        <div class="card">
          <img src="https://via.placeholder.com/600x400&amp;text=thumbnail" class="card-img-top" />
          <div class="card-body">
            <h5 class="card-title">Article title</h5>
            <p class="card-text">Ut sed ligula vel felis vulputate lobortis id eget mauris. Nullam sollicitudin arcu ac diam ornare, eget iaculis nisl accumsan.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
      </div>

      <div class="slide">
        <div class="card">
          <img src="https://via.placeholder.com/600x400&amp;text=thumbnail" class="card-img-top" />
          <div class="card-body">
            <h5 class="card-title">Article title</h5>
            <p class="card-text">Ut sed ligula vel felis vulputate lobortis id eget mauris. Nullam sollicitudin arcu ac diam ornare, eget iaculis nisl accumsan.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
      </div>

      <div class="slide">
        <div class="card">
          <img src="https://via.placeholder.com/600x400&amp;text=thumbnail" class="card-img-top" />
          <div class="card-body">
            <h5 class="card-title">Article title</h5>
            <p class="card-text">Ut sed ligula vel felis vulputate lobortis id eget mauris. Nullam sollicitudin arcu ac diam ornare, eget iaculis nisl accumsan.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
      </div>

    </div>
  </div>
</main>