【问题标题】:Transform animation not working for first slide in bootstrap carousel变换动画不适用于引导轮播中的第一张幻灯片
【发布时间】:2019-02-15 22:32:17
【问题描述】:

我有一个 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);

Here is the fiddle for the same

【问题讨论】:

    标签: twitter-bootstrap bootstrap-4 carousel css-transforms bootstrap-carousel


    【解决方案1】:

    我认为首先你不应该通过 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;
    }
    

    工作小提琴:http://jsfiddle.net/v2nc9hx3/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 2022-11-30
      相关资源
      最近更新 更多