5

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?

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:

.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;
}
Community
  • 1
  • 1
Alex
  • 34,699
  • 13
  • 75
  • 158

4 Answers4

8

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.

The CSS3 solution provided here from this Stackoverflow answer by @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):

/*
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-interval attribute, which is bigger than the transition-duration set in the above 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.

Community
  • 1
  • 1
Alex
  • 34,699
  • 13
  • 75
  • 158
3

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

.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

In the first line just remove slide class:

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

In css add:

.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;
}
2

add this

transition: opacity ease-out .7s;

on your class

.carousel.fade .item 

so it looks like

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

you can increase the timing to make it more slower

Shuhad zaman
  • 3,156
  • 32
  • 32