延迟一段时间后,一个接一个的翻转多个图像

时间:2019-02-11 10:38:47

标签: javascript html css css-animations

在我的网站中,我想创建一个CSS动画,在该动画中,我试图在延迟1秒后一个接一个地翻转多个图像,但是它不起作用。当第一张图像翻转时,第二张图像应翻转然后再翻转,依此类推

this一样,但是在载入时,每个图像都应该一个接一个地翻转。 假设有4张图片,第一张图片的延迟为0,然后第二张图片的延迟为:1,依此类推,直到第四张图片的延迟为:4

javascript

document.addEventListener("DOMContentLoaded", function() {
    var rotateComplete = function() {
    with(target.style) {
      webkitAnimationName = MozAnimationName = msAnimationName = "";
    }
    target.appendChild(arr[0]);
    setTimeout(function(el) {
      with(el.style) {
        webkitAnimationName = MozAnimationName = msAnimationName = "rotator2";
      }
    }, 0, target);
  };

  var target = document.getElementById("rotator2");
  var arr = target.getElementsByTagName("a");

  target.addEventListener("webkitAnimationEnd", rotateComplete, false);
  target.addEventListener("animationend", rotateComplete, false);
  target.addEventListener("MSAnimationEnd", rotateComplete, false);

}, false);
#stage2 {
  margin: 2em auto 1em 50%;
  height: 240px;
  -webkit-perspective: 1200px;
  -webkit-perspective-origin: 0 50%;
  -moz-perspective: 1200px;
  -moz-perspective-origin: 0 50%;
  -ms-perspective: 1200px;
  -ms-perspective-origin: 0 50%;
}

#rotator2 a {
  position: absolute;
  left: -151px;
  -moz-transform-style: preserve-3d;
}

#rotator2 a img {
  padding: 10px;
  border: 1px solid #ccc;
  background: #fff;
  -webkit-backface-visibility: hidden;
  -moz-backface-visibility: hidden;
  -ms-backface-visibility: hidden;
}

#rotator2 a:nth-child(1) img {
  -webkit-transform: rotateY(-120deg) translateZ(80px);
  -moz-transform: rotateY(-120deg) translateZ(80px);
  -ms-transform: rotateY(-120deg) translateZ(80px);
}

#rotator2 a:nth-child(2) img {
  -webkit-transform: translateZ(80px);
  -moz-transform: translateZ(80px);
  -ms-transform: translateZ(80px);
}

#rotator2 a:nth-child(3) img {
  -webkit-transform: rotateY(120deg) translateZ(80px);
  -moz-transform: rotateY(120deg) translateZ(80px);
  -ms-transform: rotateY(120deg) translateZ(80px);
}

#rotator2 a:nth-child(n+4) {
  display: none;
}

@-webkit-keyframes rotator2 {
  from {
    -webkit-transform: rotateY(0deg);
  }
  to {
    -webkit-transform: rotateY(-120deg);
  }
}

@-moz-keyframes rotator2 {
  from {
    -moz-transform: rotateY(0deg);
  }
  to {
    -moz-transform: rotateY(-120deg);
  }
}

@-ms-keyframes rotator2 {
  from {
    -ms-transform: rotateY(0deg);
  }
  to {
    -ms-transform: rotateY(-120deg);
  }
}

#rotator2 {
  -webkit-transform-origin: 0 0;
  -webkit-transform-style: preserve-3d;
  -webkit-animation-timing-function: cubic-bezier(1, 0.2, 0.2, 1);
  -webkit-animation-duration: 2s;
  -webkit-animation-delay: 1s;
  -moz-transform-origin: 0 0;
  -moz-transform-style: preserve-3d;
  -moz-animation-timing-function: cubic-bezier(1, 0.2, 0.2, 1);
  -moz-animation-duration: 2s;
  -moz-animation-delay: 1s;
  -ms-transform-origin: 0 0;
  -ms-transform-style: preserve-3d;
  -ms-animation-timing-function: cubic-bezier(1, 0.2, 0.2, 1);
  -ms-animation-duration: 2s;
  -ms-animation-delay: 1s;
}

#rotator2:hover {
  -webkit-animation-play-state: paused;
  -moz-animation-play-state: paused;
  -ms-animation-play-state: paused;
}
<div id="stage2">
  <div id="rotator2" style="-webkit-animation-name: rotator2; -moz-animation-name: rotator2; -ms-animation-name: rotator2;">
    <a href="1.jpg"><img src="img/1.jpg" width="280" alt ="1"></a>
    <a href="2.jpg"><img src="img/2.jpg" width="280" alt ="2"></a>
    <a href="3.jpg"><img src="img/3.jpg" width="280" alt ="3"></a>
    <a href="4.jpg"><img src="img/4.jpg" width="280" alt ="4"></a>
    <a href="5.jpg"><img src="img/5.jpg" width="280" alt ="5"></a>
    <a href="6.jpg"><img src="img/6.jpg" width="280" alt ="6"></a>
    <a href="7.jpg"><img src="img/7.jpg" width="280" alt ="7"></a>
    <a href="8.jpg"><img src="img/8.jpg" width="280" alt ="8"></a>
  </div>
  </div>

第一张图片在不断翻转

1 个答案:

答案 0 :(得分:1)

如我所说,如果我对您的理解正确,则可以使用animation-delay。该值为

  

(卡索引)* animation-duration

<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    body {
      font-family: Arial, Helvetica, sans-serif;
    }
    
    .flip-card {
      display: inline-block;
      background-color: transparent;
      width: 300px;
      height: 300px;
      perspective: 1000px;
    }
    
    .flip-card-inner {
      position: relative;
      width: 100%;
      height: 100%;
      text-align: center;
      transition: transform 0.6s;
      transform-style: preserve-3d;
      box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
    }
    
    .flip-card-front,
    .flip-card-back {
      position: absolute;
      width: 100%;
      height: 100%;
      backface-visibility: hidden;
    }
    
    .flip-card-front {
      background-color: #bbb;
      color: black;
      z-index: 2;
    }
    
    .flip-card-back {
      background-color: #2980b9;
      color: white;
      transform: rotateY(180deg);
      z-index: 1;
    }
    
    .flip-card .flip-card-inner {
      animation: rotate 3s .3s infinite;
    }
    
    .flip-card:nth-child(2) .flip-card-inner {
      animation-delay: .6s;
    }
    
    .flip-card:nth-child(3) .flip-card-inner {
      animation-delay: .9s;
    }
    
    .flip-card:nth-child(4) .flip-card-inner {
      animation-delay: 1.2s;
    }
    
    @keyframes rotate {
      0%, 60% {
        transform: rotateY(0);
      }
      10%, 50% {
        transform: rotateY(180deg);
      }
    }
  </style>
</head>

<body>

  <h1>Card Flip with Text</h1>
  <h3>Hover over the image below:</h3>

  <div class="cards">
    <div class="flip-card">
      <div class="flip-card-inner">
        <div class="flip-card-front">
          <img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" style="width:300px;height:300px;">
        </div>
        <div class="flip-card-back">
          <h1>John Doe</h1>
          <p>Architect & Engineer</p>
          <p>We love that guy</p>
        </div>
      </div>
    </div>
    <div class="flip-card">
      <div class="flip-card-inner">
        <div class="flip-card-front">
          <img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" style="width:300px;height:300px;">
        </div>
        <div class="flip-card-back">
          <h1>John Doe</h1>
          <p>Architect & Engineer</p>
          <p>We love that guy</p>
        </div>
      </div>
    </div>
    <div class="flip-card">
      <div class="flip-card-inner">
        <div class="flip-card-front">
          <img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" style="width:300px;height:300px;">
        </div>
        <div class="flip-card-back">
          <h1>John Doe</h1>
          <p>Architect & Engineer</p>
          <p>We love that guy</p>
        </div>
      </div>
    </div>
    <div class="flip-card">
      <div class="flip-card-inner">
        <div class="flip-card-front">
          <img src="https://www.w3schools.com/howto/img_avatar.png" alt="Avatar" style="width:300px;height:300px;">
        </div>
        <div class="flip-card-back">
          <h1>John Doe</h1>
          <p>Architect & Engineer</p>
          <p>We love that guy</p>
        </div>
      </div>
    </div>
  </div>
</body>

</html>

更新

要使其无限运行,动画应采用不同的计算方式,因为它应注意后面的动画。

计算是

  

0.3s(翻转动画)* 5(4张卡+迭代之间的延迟增加1张)* 2(来回)= 3s。

     

因此,每个“刻度”为10%。我们想在动画的中间将其翻转回50%。后面的动画刻度增加10%。

这是生命周期:

|---|---|---|---|---|---|---|---|---|---|

|___|___|___|___|___|___|___|___|___|___|
Front w   w   w   w  Back w   w   w   w
 .3s .3s .3s .3s .3s .3s .3s .3s .3s .3s 
|_______________________________________|
                   3s