【问题标题】:How to smooth transition between elements of different heights in Bootstrap carousel如何在 Bootstrap 轮播中平滑不同高度的元素之间的过渡
【发布时间】:2017-05-28 08:58:24
【问题描述】:

我正在为一个项目构建一个引导轮播。幻灯片的内容有不同的高度。目前,不同大小内容的过渡动画非常不稳定。我想知道是否有任何方法可以修复这种过渡,并在轮播在幻灯片之间调整高度时使其更加优雅/平滑。

*请注意,我想保持幻灯片之间的不同高度。以这种方式调整它们的大小是出于对项目至关重要的原因。

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

这是轮播的代码:

<!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>

【问题讨论】:

  • 添加样式 .item img{ height:300px !important;宽度:自动;}
  • 谢谢,但我想保持高度的差异。我不希望内容按比例缩小。

标签: javascript css twitter-bootstrap carousel


【解决方案1】:

由于左/右翻译,它被砍了很多,所以如果你删除它应该会好很多。

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

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

但是如果你的意思是在不同图像大小之间设置高度动画,那么你可以使用这个 jQuery:

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

CODEPEN

【讨论】:

  • 这会为我切断图像内容。我也不想缩小轮播中的内容。我想保持高度的差异。只需修复过渡,使其更平滑、更不连贯。
  • 保持高度差异,不知道你的意思。过渡很流畅,一点也不波涛汹涌。
  • 第三张幻灯片,即 160x600,被截断。如果我缩放这张幻灯片以适应 300 像素的高度,它会缩小并变小。我不希望缩放图像以适应受限的高度。我希望保持元素不同的高度,并修复在这些不同高度之间调整的波涛汹涌的轮播动画。感谢您的帮助。
  • @LizBanach 我更改了我的 CODEPEN 和答案。我试图避免左/右翻译它有很大帮助,不知道如何改进它。
  • 看起来很棒!谢谢!
【解决方案2】:

另一种你可以使用 javascript 来做到这一点的方法。

Example

在该代码中,我添加事件侦听器以侦听从引导程序触发的slide.bs.carousel,获取活动项的高度,然后为carousel-inner 元素高度设置动画。

【讨论】:

    【解决方案3】:
    <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>
    

    添加样式

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

    【讨论】:

      猜你喜欢
      • 2015-03-07
      • 2014-01-17
      • 2016-10-03
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 2014-05-08
      • 1970-01-01
      相关资源
      最近更新 更多