简体   繁体   中英

How to smooth transition between elements of different heights in Bootstrap carousel

I am building a Bootstrap carousel for a project. The content of the slides have different heights. Currently, the transition animation for the different sized content is very choppy. I am wondering if there is any way to fix this transition and make it a bit more graceful/smooth as the carousel adjusts in height between the slides.

*Please note that I would like to maintain the different heights between the slides. They are sized that way for a reason that is essential to the project.

Please see an example of the choppy transition on JS Bin.

Here is the code for the carousel:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Bootstrap Carousel</title>
  <!-- Latest compiled and minified CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <!-- Optional theme -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
  <!-- jQuery -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <!-- Latest compiled and minified JavaScript -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  </head>
<body>

<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
    <li data-target="#carousel-example-generic" data-slide-to="1"></li>
    <li data-target="#carousel-example-generic" data-slide-to="2"></li>
  </ol>

  <!-- Wrapper for slides -->
  <div class="carousel-inner" role="listbox">
    <div class="item active">
      <img style="margin: 0 auto;" src="http://placehold.it/300x300">
    </div>
    <div class="item">
      <img style="margin: 0 auto;" src="http://placehold.it/250x250">
    </div>
    <div class="item">
      <img style="margin: 0 auto;" src="http://placehold.it/160x600">
    </div>
  </div>

      <!-- Controls -->
      <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

</body>
</html>

It was chopping a lot because of the translation left/right so if u remove it it should be a lot better.

.carousel-inner .item {
  opacity: 0;
  transition: none;
  transform: translate3d(0,0,0) !important;
}

.carousel-inner .active {
  transition: opacity 1s ease-in-out;
  opacity: 1;
}

but if u meant animate the height between different image sizes than u can use this jQuery:

$('.carousel').carousel().on('slide.bs.carousel', function (e) {
    var nextH = $(e.relatedTarget).height();
    $(this).find('.active.item').parent().animate({
        height: nextH
    }, 1000);
});

CODEPEN

Another way you can use javascript to done that.

Example

In that code I add event listener for listen slide.bs.carousel that triggered from bootstrap, get height of active item then animate carousel-inner element height.

<div class="item active">
  <div class="img" style="background-image:url('http://placehold.it/300x300')"></div>
</div>
<div class="item">
  <div class="img" style="background-image:url('http://placehold.it/250x250')"></div>
</div>
<div class="item">
  <div class="img" style="background-image:url('http://placehold.it/160x600')"></div>
</div>

Add style

.item .img {
   height:600px; //Maximum height
   background-repeat:no-repeat;
   background-position:center center;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM