【问题标题】:css animation on hover does not end smoothly on mouse leave悬停时的 CSS 动画在鼠标离开时不会顺利结束
【发布时间】:2018-01-10 23:31:10
【问题描述】:

我有多个绝对定位的<svg> (!!!) 元素,初始参数为left:0top:0,还有一个div 称为btn。当我悬停btn 时,我通过jQuery 在这些svg 元素上设置动画,例如

$("#btn").hover(
    function(){
      $(this).parent().parent().find(".svg-circle").attr("class", "svg-circle zoomy");
    }, 
    function(){
      $(this).parent().parent().find(".svg-circle").attr("class", "svg-circle");
    });

这些是 css 类

.svg-circle{
  position: absolute;
  z-index: 99;
  transition: filter 300ms ease-in-out;
}
.zoomy{
  animation-name: pulse_anim;
  animation-duration: 4000ms;
  animation-iteration-count: infinite;
  animation-timing-function: linear;
}
@keyframes pulse_anim{
  0% { transform: scale(1); }
  10% { transform: scale(1.0.3); }
  20% { transform: scale(0.94); }
  30% { transform: scale(1.05); }
  40% { transform: scale(0.98); }
  50% { transform: scale(1.03); }
  60% { transform: scale(1); }
  70% { transform: scale(0.98); }
  80% { transform: scale(1); }
  80% { transform: scale(1.07); }
  100% { transform: scale(1); }
}

这些会在这些svg 元素上触发一个漂亮的脉冲动画,但是当我用鼠标离开btn 时,svg 元素会立即缩放到它们的初始大小,而不会平滑过渡。有没有办法告诉动画它已经结束并且应该顺利过渡到初始大小?

https://jsfiddle.net/qrw1fgh8/

【问题讨论】:

  • 你可以做一个小提琴吗?
  • 这是一个普遍的理论问题,但我会做一个

标签: jquery html css animation svg


【解决方案1】:

也许你可以使用动画animation-play-state 属性来暂停你的动画:

.svg-circle{
  position: absolute;
  z-index: 99;
  transition: filter 300ms ease-in-out, transform 1s;
  animation-name: pulse_anim;
  animation-duration: 4000ms;
  animation-iteration-count: 1;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
  animation-play-state: paused;
}
.zoomy{
  animation-play-state: running;
}

https://jsfiddle.net/bgqz1h81/

【讨论】:

  • 如此简单,但正是我搜索的内容!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 2012-05-04
  • 2022-09-24
  • 2012-01-23
  • 2014-09-22
相关资源
最近更新 更多