【问题标题】:how to animate 10 consecutive elements using jquery如何使用 jquery 为 10 个连续元素设置动画
【发布时间】:2018-10-10 04:13:53
【问题描述】:

我有 10 个 <i> 元素,它们是 <div> 的子元素,它们都显示为一个白色圆圈。 我喜欢通过 jquery 创建一个动画,将它们放大 x3,然后再缩小到原始大小。这本身就很容易,但我想做的是:

让它开始缩放,然后在完成之前(比如说通过其过渡或变换的三分之一)开始下一个元素等等。

一旦到达终点,重新开始。遍历所有元素的单个循环应该是 3 秒。

希望有人能指出我正确的方向。

[更新] 我喜欢使用 jquery .animate() 功能,而不是使用关键帧和 css 动画

.spinner {
  background-color: black; /*So you can see the circles*/
}

.spinner i {
  display: block;
  position: absolute;
  opacity: 1;
}

.spinner i b {
  display: block;
  width: 6px;
  height: 6px;
  border-radius: 6px;
  background: white;
  box-shadow: 0px 0px 14px white;
}
<div id="spinner" class="spinner">
  <i><b></b></i><!--1-->
  <i><b></b></i><!--2-->
  <i><b></b></i><!--3-->
  <i><b></b></i><!--4-->
  <i><b></b></i><!--5-->
  <i><b></b></i><!--6-->
  <i><b></b></i><!--7-->
  <i><b></b></i><!--8-->
  <i><b></b></i><!--9-->
  <i><b></b></i><!--10-->
</div>

【问题讨论】:

    标签: jquery html css jquery-animate


    【解决方案1】:

    我想这就是你所追求的。我已经记录在源代码中。

    $(".spinner i").each( function(index, element) {
      const delay = 0.3*index; /* Calculate delay for current element */
      $(element).attr("style","animation-delay:"+delay+"s"); /* Apply delay */
      $(element).addClass("anim"); /* Start the animation */
    });
    .spinner {
      background-color: black;
      height: 100vh;
      /*So you can see the circles*/
    }
    
    .spinner i {
      display: block;
      position: absolute;
      opacity: 1;
    }
    
    .spinner i b {
      display: block;
      width: 6px;
      height: 6px;
      border-radius: 6px;
      background: white;
      box-shadow: 0px 0px 14px white;
    }
    /* Class to define the settings for the animation */
    .spinner i.anim {
      animation-name: scale;
      animation-duration: 0.3s; /* Total duration / 10 */
      animation-iteration-count: infinite; /* Repeat forever */
    }
    /* Animation of the scale */
    @keyframes scale {
        0%, 100%  {transform: scale(1);}
        50%  {transform: scale(3);}
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="spinner" class="spinner">
      <i><b></b></i>
      <!--1-->
      <i><b></b></i>
      <!--2-->
      <i><b></b></i>
      <!--3-->
      <i><b></b></i>
      <!--4-->
      <i><b></b></i>
      <!--5-->
      <i><b></b></i>
      <!--6-->
      <i><b></b></i>
      <!--7-->
      <i><b></b></i>
      <!--8-->
      <i><b></b></i>
      <!--9-->
      <i><b></b></i>
      <!--10-->
    </div>

    【讨论】:

    • 嗨 Gerard,看起来不错,但是我想避免关键帧和 css3 动画。只需将 jquery 与 .animate() 一起使用即可。不过,我仍然想保留这篇文章作为答案
    • 好的。请记住,简单的 CSS3 动画通常比 jQuery 动画更快,因为它们可以在不同的线程上运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2015-05-16
    • 2019-01-11
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    相关资源
    最近更新 更多