【问题标题】:SVG shake animation on hover悬停时的 SVG 抖动动画
【发布时间】:2019-01-14 11:54:53
【问题描述】:

当您将鼠标悬停在 svg img 上时,我正在尝试使用 CSS 关键帧进行抖动。

这是我想要达到的效果:https://www.w3schools.com/code/tryit.asp?filename=FZ7CQHXPG42J

有一些 javascript 可以为相同的 svg 设置动画,但它以父 div #ellipse 为目标,当单击汉堡包图标时,它会从左向右移动。我看不出它会如何干扰,但我添加它只是为了确保。

我在下面添加了相关代码。完整代码见官网。

// Other unrelated(!?) animations on #ellipse //

function moveEllipseRight() {
  ellipse.animate([
    // keyframes
    {
      transform: 'translateX(0px)'
    },
    {
      transform: 'translateX(' + width + 'px)'
    }
  ], {
    // timing options
    duration: 500,
    iterations: 1,
    easing: 'ease-in-out',
    fill: 'forwards'
  });
}

function moveEllipseLeft() {
  ellipse.animate([
    // keyframes
    {
      transform: 'translateX(' + width + 'px)'
    },
    {
      transform: 'translateX(0px)'
    }
  ], {
    // timing options
    duration: 500,
    iterations: 1,
    easing: 'ease-in-out',
    fill: 'forwards'
  });
}
#ellipse {
  position: absolute;
  top: 120px;
  z-index: -99;
  animation: 3s linear 0s slide 1;
  left: -200px;
}

img.shake:hover {
  animation: shake 0.5s;
  animation-iteration-count: infinite;
}

@keyframes shake {
  0% {
    transform: translate(1px, 1px) rotate(0deg);
  }
  10% {
    transform: translate(-1px, -2px) rotate(-1deg);
  }
  20% {
    transform: translate(-3px, 0px) rotate(1deg);
  }
  30% {
    transform: translate(3px, 2px) rotate(0deg);
  }
  40% {
    transform: translate(1px, -1px) rotate(1deg);
  }
  50% {
    transform: translate(-1px, 2px) rotate(-1deg);
  }
  60% {
    transform: translate(-3px, 1px) rotate(0deg);
  }
  70% {
    transform: translate(3px, 1px) rotate(-1deg);
  }
  80% {
    transform: translate(-1px, -1px) rotate(1deg);
  }
  90% {
    transform: translate(1px, 2px) rotate(0deg);
  }
  100% {
    transform: translate(1px, -2px) rotate(-1deg);
  }
}
<div id="ellipse">
  <img src="https://lh5.googleusercontent.com/-yqttzktPkDY/AAAAAAAAAAI/AAAAAAAABGU/z6CVGRmY-C8/photo.jpg?sz=328" alt="ellipse" class="shake" width="400" height="400" />
</div>

【问题讨论】:

  • 看起来工作正常,可能是因为其他原因?

标签: css animation svg


【解决方案1】:

问题在于您的#ellipse 风格。

#ellipse {
    position: absolute;
    top: 120px;
    z-index: -99; /* remove this, it is not doing anything useful. */ 
    animation: 3s linear 0s slide 1;
    left: -200px;
}

问题是,悬停根本没有被触发,因为它在 container 元素后面,因为 z-index 是负数。这个负 z 索引根本没有用,除非您打算将文本放在图像上方,而我在您的网站上看不到。

【讨论】:

  • 您好 Prajwal,谢谢!这似乎是个问题!我放了一个 z-index,因为我希望当您单击导航时对象从左向右移动时,它会在文本后面滑动。导航仍然在顶部,因此当
    打开时它仍然会阻止动画,因为它位于#ellipse 的顶部,但我可以忍受。
猜你喜欢
  • 2012-05-26
  • 2021-06-08
  • 1970-01-01
  • 2021-02-24
  • 1970-01-01
  • 2014-09-28
  • 2021-02-02
  • 2015-01-12
  • 2016-05-09
相关资源
最近更新 更多