Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Bootstrap Carousel fade transition

There are several questions asked on Stackoverflow regarding Carousel fade transition but none of them seem to work on a default 4.0.0 implementation:

<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
  <ol class="carousel-indicators">
    <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
  </ol>
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img class="d-block w-100" src="..." alt="First slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="..." alt="Second slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="..." alt="Third slide">
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

Did bootstrap change the way of transitioning carousel-item -s since Alpha.6 version? How would one go about implementing the fade transition instead of slide in 4.0.0 ?

like image 799
astralmaster Avatar asked Feb 07 '18 13:02

astralmaster


People also ask

How do I change the transition of a bootstrap carousel?

So, after the introduction let's change the transition of our carousel. As specified here to use the fade transition instead of the slide transition is very easy, just add the class “carousel-fade” to the carousel's first “div”. This is a free Carousel template made with the Bootstrap 4 framework.

How do I fade an image in bootstrap?

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

Which attribute will you use to set a carousel change time interval?

The Data-interval attribute is used to set the interval time between two carousel item by default its value is 3000 milliseconds.

How do I stop carousel interval?

Use the [interval]="'0'" input. This will disable the auto-sliding feature.


2 Answers

Bootstrap 5 Carousel Fade (update 2021)

Bootstrap 5 includes a "fade" effect that can be used by simply adding the carousel-fade class to the Carousel. By default, the transition duration is .6s. To increase the duration, and make the fade between slides slower, override the transition timing on the CSS...

/* change transition duration to control the speed of fade effect */
.carousel-item {
  transition: transform 2.6s ease-in-out;
}

.carousel-fade .active.carousel-item-start,
.carousel-fade .active.carousel-item-end {
  transition: opacity 0s 2.6s;
}

Bootstrap 5 Carousel Fade Slower


Bootstrap 4.0 Carousel Fade (original answer)

.carousel-fade .carousel-item {
 opacity: 0;
 transition-duration: .6s;
 transition-property: opacity;
}

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

.carousel-fade .active.carousel-item-left,
.carousel-fade  .active.carousel-item-right {
 opacity: 0;
}

.carousel-fade  .carousel-item-next,
.carousel-fade .carousel-item-prev,
.carousel-fade .carousel-item.active,
.carousel-fade .active.carousel-item-left,
.carousel-fade  .active.carousel-item-prev {
 transform: translateX(0);
 transform: translate3d(0, 0, 0);
}

The fade effect was removed from the carousel during the 4.0 Beta and is also not available in 4.0.0. This pull request indicates that the fade effect will return in 4.1 or 4.2. In the meanwhile, the above CSS will work for 4.0.0

https://codeply.com/go/LhLJlldsLN

like image 152
Zim Avatar answered Oct 21 '22 01:10

Zim


To get the fade-in effect, you can add "carousel-fade" class to carousel's main div as below.


Update: Bootstrap 5

Bootstrap 5 is almost the same with Bootstrap 4 but the data-ride attribute is now data-bs-ride.

Bootstrap 5 example

<div id="carouselExampleIndicators" class="carousel slide carousel-fade" data-bs-ride="carousel">
...
</div>

Bootstrap 4 example

<div id="carouselExampleIndicators" class="carousel slide carousel-fade" data-ride="carousel"> 
....
</div>
like image 19
ronaldtgi Avatar answered Oct 21 '22 03:10

ronaldtgi