CSS动画后hover效果失效

3

我有一个基本按钮,最初我对它进行了动画处理。但是一旦动画完成后,我想在悬停时添加新的动画效果;但由于某种原因似乎无法实现。

例如:

按钮动画:

.container {
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  max-height: 100%;
  overflow: hidden;
}

.slide-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 100%;
  background-color: #feffff;
  box-shadow: 0 2px 20px 0 #686f7638;
  margin-top: 10px;
  width: 45px;
  height: 45px;
  animation: testing 1s ease-in forwards;
}

.slide-btn:hover {
  transform: scale(1.5);
}

@keyframes testing {
  to {
    transform: translateX(100px);
  }
}
<div class="container">
  <div class="slide-btn">></div>
</div>

我猜测对于我正在使用的 CSS 动画,我需要 forwards,但实际上我真的需要 forwards


如果我理解正确:按钮会不断向右移动,当鼠标悬停时,它会缩放1.5倍。在您理想的情况下:您是否希望按钮在悬停时继续移动(最终导致取消悬停状态)?悬停后动画是否仍在进行中(以scale = 1未悬停状态)? - Al-un
2个回答

1

是的,这是因为 forwards 会覆盖变换动画。你可以像下面这样做,而不是使用 forwards

.container {
  width: 100%;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  max-height: 100%;
  overflow: hidden;
}

.slide-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 100%;
  background-color: #feffff;
  box-shadow: 0 2px 20px 0 #686f7638;
  margin-top: 10px;
  width: 45px;
  height: 45px;
  transform: translateX(100px);
  transition:0.5s;
  animation: testing 1s ease-in;
}

.slide-btn:hover {
  transform: translateX(100px) scale(1.5);
}

@keyframes testing {
  from {
    transform: translateX(0px);
  }
}
<div class="container">
  <div class="slide-btn">></div>
</div>


我试图避免设置初始转换值。这就是为什么我需要前向传递的原因。还有其他方法吗? - Doodoo
@Doodoo,你想要鼠标悬停时有过渡效果吗?如果你不需要过渡效果,我还有另一种使用forwards的方法。 - Temani Afif
是的,我需要在悬停时进行过渡,并使用forwards进行初始动画。 - Doodoo
1
你是否已经找到了解决方法? - Ben Clarke

0

在'.slide-btn:hover'中加入'animation-fill-mode: none'怎么样?

.slide-btn:hover {
    animation-fill-mode: none;
    transform: scale(1.5);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接