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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29169823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 03:05:57  来源:igfitidea点击:

Bootstrap Carousel - how to fade between slides slowly

csstwitter-bootstrap

提问by Alex

I'm using the latest Bootstrap Carousel and need to fade slowly (about 5s) between slides. I've looked at a number of examples, and have tried to implement this one. The problem is that the fade between slides is too quick (less than a second). How do I change that to a longer fade?

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

Here's my code:

这是我的代码:

<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>

Here's the CSS:

这是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;
}

回答by Alex

The solution was non-trivial, as my late, great computer science professor used to say. The Slick Carousel(a derivative of the Bootstrap Carousel) uses class="sld-transition-2"(one of three available classes) on the body tag to set the carousel's transition. This was colliding with the fade transition I was trying to set. So it was removed.

正如我已故的伟大的计算机科学教授曾经说过的那样,解决方案非常重要。的油滑旋转木马(衍生物的引导轮播的)的用途class="sld-transition-2"在主体标签(三个可用类之一)来设定转盘的过渡。这与我试图设置的淡入淡出过渡相冲突。所以它被删除了。

The CSS3 solution provided herefrom this Stackoverflow answerby @transportedman was the clincher. I made some modifications to it for our requirements and placed it in the last CSS file to be loaded for the site (to override everything that came before it):

所提供的解决方案CSS3这里从这个#1的答案由@transportedman是硬道理。我根据我们的要求对其进行了一些修改,并将其放在要为站点加载的最后一个 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);
    }
}

Last but not least, modified the carousel DIV tag to have the data-intervalattribute, which is bigger than the transition-durationset in the above CSS3:

最后但并非最不重要的是,修改了 carousel DIV 标签,使其具有data-intervaltransition-duration上述 CSS3 中的集合更大的属性:

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

Hope this helps others who run into similar issues.

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

回答by fubbe

Since the link provided by @Turbojohan does not work anymore. The below code from the deprecated link. Just exchange slide class attribute with fade.

由于@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;
}

回答by Spencodromix

In the first line just remove slideclass:

在第一行中删除slide类:

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

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

In css add:

在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;
}