如何将 Bootstrap 4 Carousel 更改为淡入淡出过渡而不是滑动
作者:admin 时间:2021-9-27 19:12:16 浏览:Bootstrap 4 Carousel 的幻灯片默认是滑动过渡的,如果你想更改为淡入淡出过渡,那么可以参考下本文介绍的方法。
为此,我们需要对 HTML 代码进行的唯一更改是将类添加carousel-fade
到主轮播 div
。在我们的示例中,它是带有 ID 元素的 div
my-carousel
。接下来,我们只需复制并粘贴下面的 CSS 代码,就可以了!
HTML
<div id="my-carousel" class="carousel slide carousel-fade" data-ride="carousel" data-interval="5000">
<ol class="carousel-indicators">
<li data-target="#my-carousel" data-slide-to="0" class="active"></li>
<li data-target="#my-carousel" data-slide-to="1"></li>
<li data-target="#my-carousel" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="1.jpg" alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="2.jpg" alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="3.jpg" alt="Third slide">
</div>
</div>
<a class="carousel-control-prev" href="#my-carousel" 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="#my-carousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
CSS
.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);
}
很简单吧,希望这对你有所帮助!
您可能对以下文章也感兴趣
相关文章
x
- 站长推荐