Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Carousel - how to fade between slides slowly

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;
}
like image 482
Alex Avatar asked Mar 20 '15 15:03

Alex


People also ask

How do I slow down bootstrap carousel?

If you are using bootstrap carousel, then you need to provide the data-interval attribute on your main carousel div to change the speed of carousel. Ex. Here the value 3000 is in msec. If you are not using bootstrap carousel, then specify more details what exactly you have done so far.

How do you do a carousel fade?

Step 1: Add bootstrap CDN to your HTML code. Step 2: For making a bootstrap carousel you have to add class = “carousel” in your HTML div box. Step 3: To create the carousel fade in and fade out transition instead of a slider you have to add a class=”carousel-fade”.

How do you change the transition duration of the carousel component?

The transition duration of . carousel-item can be changed with the $carousel-transition-duration Sass variable before compiling or custom styles if you're using the compiled CSS. If multiple transitions are applied, make sure the transform transition is defined first (eg. transition: transform 2s ease, opacity .

How do I change the timing of a bootstrap carousel slide?

You can simply use the data-interval attribute of the carousel class. It's default value is set to data-interval="3000" i.e 3seconds. All you need to do is set it to your desired requirements.


1 Answers

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.

like image 66
Alex Avatar answered Sep 24 '22 14:09

Alex