【问题标题】:Animate an element to left and right like a yoyo [closed]像溜溜球一样左右动画元素[关闭]
【发布时间】:2020-12-08 23:39:22
【问题描述】:

我有一个红色框作为动画元素。

这是我想如何为红色框设置动画的简单表示。

这是一个尝试,但正如您所见,运动的锚点位于框的左侧,而不是我希望的中心:

.yo-yo {
  display: block;
  position: absolute;
  height: 50px;
  width: 200px;
  background: red; 
  transform-origin: 50% 50%;
  animation: yo-yo 0.5s infinite alternate;  /* Animation speed and type */
}

/* Animation beginning and ending */
@keyframes yo-yo {
  from {  left: 0 }
  to {  left: 20px }
}
<span class="yo-yo"></span>

这是 TweenMax 特定版本的脚本标签:

<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.1.3/TweenMax.min.js" integrity="sha512-DkPsH9LzNzZaZjCszwKrooKwgjArJDiEjA5tTgr3YX4E6TYv93ICS8T41yFHJnnSmGpnf0Mvb5NhScYbwvhn2w==" crossorigin="anonymous"></script>

【问题讨论】:

标签: javascript jquery css gsap


【解决方案1】:

您需要使用progress,而不是简单地在关键帧规则中声明fromto

这样的事情就足够了。如果你想要一个重复的动画,那么只需在线性之前添加一个infinite

html,
body {
  padding: 0;
  margin: 0;
  outline: none;
  border: 0;
}

.yo-yo {
  display: block;
  position: absolute;
  height: 50px;
  width: 200px;
  background: red;
  transform-origin: 50% 50%;
  animation: yo-yo 2s linear;
  /* Animation speed and type */
  animation-timing-function: ease-in-out;
}


/* Animation beginning and ending */

@keyframes yo-yo {
  0% {
    left: 0px;
  }
  25% {
    left: -100px;
  }
  50% {
    left: 0px;
  }
  75% {
    left: 100px;
  }
  100% {
    left: 0px;
  }
}
&lt;span class="yo-yo"&gt;&lt;/span&gt;

【讨论】:

  • @Sara Ree 您可以尝试在重复动画之前添加一点延迟
  • 那么缓动没有解决办法?
  • @SaraRee 我不相信放宽会解决问题
  • @SaraRee 解决了,卡顿是由于文档默认的 padding 和 margin 导致的,再次运行试试
  • @SaraRee 也添加了缓动
【解决方案2】:

你把它弄得太复杂了。只需使用@keyframes 中的百分比,然后像这样使用transform: translateX(number)

@keyframes yo-yo {
  0% {transform: translateX(0); }
  25% { transform: translateX(-20px); }
  75% { transform: translateX(20px);  }
  100% { transform: translateX(0px);  }
}

您应该尝试使用transform 而不是给出位置。为了更好地理解,请查看此链接CSS translation vs changing absolute positioning values

【讨论】:

  • 好的,但是你的代码有问题。重复动画时的抽搐动作...
  • 尝试使用 2% 代替 0% 和 98% 代替 100%。它还会发生吗?@SaraRee
【解决方案3】:

您可以通过infinite normal linear 使用动画。如果您从动画中删除infinite,那么它将在一次迭代后停止。您的动画分为 5 个部分。 0, all the way right, 0, all the way left, 0。所以从0%, 25%, 50%, 75% and 100%开始改变左边的位置。

.yoyo-container{
  background-color: #DEEBF7;
  padding:12px;
  position: relative;
  width: 200px;
  height: 16px;
  
  
  margin: 0 auto;
}

.yo-yo {
  display: block;
  position: absolute;
  height: 16px;
  width: 200px;
  background: red; 
  transform-origin: 50% 50%;
  animation: yo-yo 2s infinite normal linear;  /* Animation speed and type */
}

/* Animation beginning and ending */
@keyframes yo-yo {
   0% {transform: translateX(0); }
  25% { transform: translateX(50%); }
  50% { transform: translateX(0);  }
  75% { transform: translateX(-50%);  }
  100% { transform: translateX(0);  }
}
<div class="yoyo-container">
<span class="yo-yo"></span>
</div>

【讨论】:

  • 我们不能使用缓动吗?
  • linearanimation-timing-function。您可以尝试使用easeease-inease-in-out 等,但linear 符合您的要求
  • 当你说 easing 时,你的意思是 tweenmax easing?
  • 请看一下这个codepen:我的意思是像这样缓和不是线性运动。:codepen.io/pixy-dixy/pen/poyEVWb
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-05
  • 2013-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-21
相关资源
最近更新 更多