Bootstrap Carousel - 如何缓慢地在幻灯片之间淡入淡出

时间:2015-03-20 15:11:13

标签: css3 twitter-bootstrap

我正在使用最新的Bootstrap Carousel,需要在幻灯片之间缓慢淡化(约5s)。我已经看了很多例子,并试图实现这个one。问题是幻灯片之间的淡入淡出太快(不到一秒钟)。如何将其更改为更长的淡入淡出?

这是我的代码:

<div class="carousel slide" id="slideshow-carousel-1" data-ride="carousel">
    <div class="carousel-inner" role="listbox">
        <!-- Slide 1 -->
        <div class="item item-1 slide-image active">
            <img class="img-responsive bg-item parallax" data-speed="1" data-width="1980" data-height="1485" src="~/images/slideshow/bottle1.png" alt="Slide 1 Background">
            <div class="carousel-caption">
                <h1 class="title">Caption 1</h1>
            </div>
        </div>
        <!-- End Slide 1 -->

        <!-- Slide 2 -->
        <div class="item item-2 slide-image">
            <img class="img-responsive bg-item parallax" data-speed="1" data-width="1980" data-height="1485" src="~/images/slideshow/bottle2.jpg" alt="Slide 2 Background">
            <div class="carousel-caption">
                <h1 class="title">Or Bottle!</h1>
            </div>
        </div>
        <!-- End Slide 2 -->
    </div>
</div>

这是CSS:

.carousel-inner > .item {
  opacity: 0;
  top: 0;
  left: 0;
  width: 100%;
  display: block;
  position: absolute;
  z-index: 0;
  -webkit-transition: opacity 5s ease;
       -o-transition: opacity 5s ease;
          transition: opacity 5s ease;
  -webkit-transform: translate3d(0, 0, 0) !important;
          transform: translate3d(0, 0, 0) !important;
}
.carousel-inner > .item:first-of-type {
  position: relative;
}
.carousel-inner > .active {
  opacity: 1;
  z-index: 3;
}
.carousel-inner > .next.left,
.carousel-inner > .prev.right {
  -webkit-transition: opacity 0.6s ease-in-out;
       -o-transition: opacity 0.6s ease-in-out;
          transition: opacity 0.6s ease-in-out;
  opacity: 1;
  left: 0;
  z-index: 2;
}
.carousel-inner > .active.left,
.carousel-inner > .active.right {
  z-index: 1;
}
.carousel-control {
  z-index: 4;
}

5 个答案:

答案 0 :(得分:8)

解决方案非常重要,正如我已故的伟大的计算机科学教授所说的那样。 Slick Carousel(Bootstrap Carousel的衍生物)使用body标签上的class="sld-transition-2"(三个可用类别之一)来设置轮播的转换。这与我试图设置的淡入淡出过渡相撞。所以它被删除了。

由@transportedman提供的来自此Stackoverflow here的{3}的CSS3解决方案是关键。我根据我们的要求对其进行了一些修改,并将其放在最后一个要为网站加载的CSS文件中(覆盖之前的所有内容):

/*
inspired from http://codepen.io/Rowno/pen/Afykb 
https://stackoverflow.com/questions/26770055/bootstrap-carousel-fade-no-longer-working-with-maxcdn-3-3-bootstrap-min-css
*/
.carousel-fade .carousel-inner .item {
  opacity: 0;
  transition-property: opacity;
  transition-duration: 4s;
  transition-timing-function:linear;
}

.carousel-fade .carousel-inner .active {
  opacity: 1;
}

.carousel-fade .carousel-inner .active.left,
.carousel-fade .carousel-inner .active.right {
  left: 0;
  opacity: 0;
  z-index: 1;
}

.carousel-fade .carousel-inner .next.left,
.carousel-fade .carousel-inner .prev.right {
  opacity: 1;
}

.carousel-fade .carousel-control {
  z-index: 2;
}

/*
WHAT IS NEW IN 3.3: "Added transforms to improve carousel performance in modern browsers."
now override the 3.3 new styles for modern browsers & apply opacity
*/
@media all and (transform-3d), (-webkit-transform-3d) {
    .carousel-fade .carousel-inner > .item.next,
    .carousel-fade .carousel-inner > .item.active.right {
      opacity: 0;
      -webkit-transform: translate3d(0, 0, 0);
              transform: translate3d(0, 0, 0);
    }
    .carousel-fade .carousel-inner > .item.prev,
    .carousel-fade .carousel-inner > .item.active.left {
      opacity: 0;
      -webkit-transform: translate3d(0, 0, 0);
              transform: translate3d(0, 0, 0);
    }
    .carousel-fade .carousel-inner > .item.next.left,
    .carousel-fade .carousel-inner > .item.prev.right,
    .carousel-fade .carousel-inner > .item.active {
      opacity: 1;
      -webkit-transform: translate3d(0, 0, 0);
              transform: translate3d(0, 0, 0);
    }
}

最后但并非最不重要的是,修改了轮播DIV标记,使其具有data-interval属性,该属性大于上述CSS3中设置的transition-duration

<div class="carousel slide carousel-fade" id="slideshow-carousel-1" data-ride="carousel" data-interval="7000">
.....
</div>

希望这可以帮助遇到类似问题的其他人。

答案 1 :(得分:5)

尝试了很多解决方案,我发现这正是我所寻求的: http://www.1squarepear.com/adding-a-responsive-bootstrap-image-carousel-that-fades-instead-of-slides/

这里幻灯片实际上相互淡化,而不是先淡出,然后切换到下一个然后淡入。

答案 2 :(得分:2)

由于@Turbojohan提供的链接不再起作用。以下代码来自不推荐使用的链接。只需用淡入淡出来交换幻灯片类属性。

.carousel.fade {
  opacity: 1;
}
.carousel.fade .item {
  transition: opacity ease-out .7s;
  left: 0;
  opacity: 0; /* hide all slides */
  top: 0;
  position: absolute;
  width: 100%;
  display: block;
}
.carousel.fade .item:first-child {
  top: auto;
  opacity: 1; /* show first slide */
  position: relative;
}
.carousel.fade .item.active {
  opacity: 1;
}

答案 3 :(得分:1)

在第一行中,只需删除slide类:

<div class="carousel" id="slideshow-carousel-1" data-ride="carousel">

在CSS中添加:

.carousel .slide-image.active {
  opacity: 1;
  transition: opacity 1s ease-in;
}

.carousel .slide-image {
  opacity: 0;
  display: block;
  left: 0;
  transition: opacity 1s ease-out;
}

答案 4 :(得分:1)

添加此

transition: opacity ease-out .7s;

在课堂上

.carousel.fade .item 

看起来像

.carousel.fade .item {
  transition: opacity ease-out .7s;
}

您可以增加时间以使其变慢