Bootstrap v4:仅文本轮播仅在滑动完成时居中文本(否则粘在顶部)

时间:2018-05-20 04:51:31

标签: javascript html css twitter-bootstrap

任何想法为什么这个纯文本旋转木马(Bootstrap v4)保持我的旋转木马项目文本贴在顶部然后居中对齐它而不是直接对齐项目的文本?

<div id="myTextOnlyCarousel" class="carousel slide mt-5" data-ride="carousel">
  <ol class="carousel-indicators">
    <li data-target="#myTextOnlyCarousel" data-slide-to="0" class="active"></li>
    <li data-target="#myTextOnlyCarousel" data-slide-to="1"></li>
    <li data-target="#myTextOnlyCarousel" data-slide-to="2"></li>
  </ol>
  <div class="carousel-inner bg-dark text-light rounded text-center d-flex h-100 align-items-center justify-content-center" style="min-height: 300px;">
    <div class="carousel-item active">
      <h1>This is AWESOME!</h1>
    </div>
    <div class="carousel-item">
      <h1>This is freaking AMAZING!</h1>
    </div>
    <div class="carousel-item">
      <h1>YEAHHH</h1>
    </div>
  </div>
  <a class="carousel-control-prev" href="#myTextOnlyCarousel" 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="#myTextOnlyCarousel" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

2 个答案:

答案 0 :(得分:1)

好的,这是因为carousel-item类上的活动类是动画中的文本幻灯片。因此,将其更改为显示块并添加从旋转木马移除flex。

.carousel-item .text{
  display: flex;
  height: 100%!important;
  min-height: 300px;
  align-items: center;
  justify-content: center;
}

.carousel-item.active,
.carousel-item-next,
.carousel-item-prev{
    display:block;
}
<div id="myTextOnlyCarousel" class="carousel slide mt-5" data-ride="carousel">
  <ol class="carousel-indicators">
    <li data-target="#myTextOnlyCarousel" data-slide-to="0" class="active"></li>
    <li data-target="#myTextOnlyCarousel" data-slide-to="1"></li>
    <li data-target="#myTextOnlyCarousel" data-slide-to="2"></li>
  </ol>
  <div class="carousel-inner bg-dark text-light rounded text-center  h-100  " style="min-height: 300px;background: red;">
    <div class="carousel-item active">
      <div class="text">
        <h1>This is AWESOME!</h1>
      </div>
    </div>
    <div class="carousel-item">
      <div class="text">
        <h1>This is freaking AMAZING!</h1>
      </div>
    </div>
    <div class="carousel-item">
      <div class="text">
        <h1>YEAHHH</h1>
      </div>
    </div>

  </div>
  <a class="carousel-control-prev" href="#myTextOnlyCarousel" 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="#myTextOnlyCarousel" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

答案 1 :(得分:0)

这是我的解决方案 JSFiddle

我已经用h1课程为你的wrapper做了准备。

我必须提供 min-height ,因为没有它,这些项目会粘在顶部。

.wrapper {
  min-height:300px;
  display: flex;
  align-items: center;
  justify-content: center;
}

并删除了一些应用于.carousel-inner div

的flex类