最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to smooth transition between elements of different heights in Bootstrap carousel - Stack Overflow

programmeradmin1浏览0评论

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 piled and minified CSS -->
  <link rel="stylesheet" href=".3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <!-- Optional theme -->
  <link rel="stylesheet" href=".3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
  <!-- jQuery -->
  <script src=".1.1/jquery.min.js"></script>
  <!-- Latest piled and minified JavaScript -->
  <script src=".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="">
    </div>
    <div class="item">
      <img style="margin: 0 auto;" src="">
    </div>
    <div class="item">
      <img style="margin: 0 auto;" src="">
    </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>

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 piled and minified CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <!-- Optional theme -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
  <!-- jQuery -->
  <script src="https://ajax.googleapis./ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <!-- Latest piled and minified JavaScript -->
  <script src="https://maxcdn.bootstrapcdn./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>
Share Improve this question edited Jan 13, 2017 at 4:07 Liz asked Jan 13, 2017 at 3:28 LizLiz 1,0485 gold badges23 silver badges51 bronze badges 2
  • add style .item img{ height:300px !important; width:auto;} – Syam Mohan M P Commented Jan 13, 2017 at 3:44
  • Thank you, but I'd like to maintain the differences in height. I do not want the content to be scaled down. – Liz Commented Jan 13, 2017 at 3:46
Add a ment  | 

3 Answers 3

Reset to default 6

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论