我有一个bootstrap 4.0轮播,我想要每个幻灯片活动时缩放效果。我用transform: scale
属性来实现它。我已经将transform: scale
应用于.carousel-item.active img
元素。我已经使用jquery为第一个元素添加了活动类。我的第一张幻灯片中的动画不起作用。当它循环时它起作用。我试图在setTimeOut函数中添加该类,但它没有帮助。谁能帮帮我吗?
这些是我的代码
HTML
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item">
<img class="d-block w-100" src="https://placeimg.com/640/480/any" alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="https://placeimg.com/640/480/nature" alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="https://placeimg.com/640/480/animals" alt="Third slide">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" 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="#carouselExampleControls" 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-item img{
transition: all 1s;
transform:scale(1);
}
.carousel-item.active img{
transform:scale(1.2);
}
jQuery的
window.setTimeout(function(){
$('#carouselExampleControls .carousel-item:first-child').addClass('active');
},2000);
我想首先你不应该用JavaScript改变“活动”类,总是先尝试找到一个CSS解决方案,然后将活动类放在HTML中,如下所示:
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="https://placeimg.com/640/480/any" alt="First slide">
</div>
另外我在某处读到使用动画而不是变换需要更少的CPU处理,所以去关键帧,如下所示:
@-webkit-keyframes zoom {
from {
-webkit-transform: scale(1, 1);
}
to {
-webkit-transform: scale(1.2, 1.2);
}
}
@keyframes zoom {
from {
transform: scale(1, 1);
}
to {
transform: scale(1.2, 1.2);
}
}
然后是css:
.carousel-inner .carousel-item.active > img {
-webkit-animation: zoom 1s forwards;
animation: zoom 1s forwards;
}