【问题标题】:CSS hover transform on animating element动画元素上的 CSS 悬停变换
【发布时间】:2020-06-26 04:00:36
【问题描述】:

我正在尝试对:hover 执行简单的 CSS 转换 — 这通常显然是一项简单的任务,但我正在尝试在动画 div 元素上执行此操作。使用@keyframes{} 的简单 CSS 动画在 Y 轴上无限动画化该元素,但是当我尝试将鼠标悬停在该元素上时没有任何反应。

如果我在悬停代码上使用!important,我可以将其完成种类的工作,但转换/缩放会立即发生,而不是使用我应用的 300 毫秒 transition 属性到.box 类。

我是否遗漏了一些明显的东西,或者这不可能?本质上,我只希望元素使用transition 效果和时间在悬停时缩放,然后在未悬停时恢复它的原始动画。谢谢

.box {
  width: 50%;
  border: solid 3px #555;
  animation: box-move 1s infinite alternate-reverse;
  transition: transform 300ms;
}

.box:hover {
  transform: scale(1.2);
}

@keyframes box-move {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(-5px);
  }
}
<div class="box">I'm a box. I move up and down, but I don't scale nicely when hovered like I should :(</div>

【问题讨论】:

  • 在应用缩放变换的地方添加另一个包装器
  • @TemaniAfif 想法很聪明,但由于某种原因,这会在变换原点产生一个奇怪的问题,并且它会在看起来有问题的奇怪方向上缩放/移动。不过还是谢谢

标签: html css css-animations css-transforms


【解决方案1】:

因为您在悬停和动画中都使用了变换属性。 试试这个。

.box {
  width: 50%;
  border: solid 3px #555;
  animation: box-move 1s infinite alternate-reverse;
  transition: transform 300ms;
}

.box:hover {
  animation: box-move-anim 1s infinite alternate-reverse;
}

@keyframes box-move {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(-5px);
  }
}
@keyframes box-move-anim {
  0% {
    transform: translateY(0) scale(1);
  }
  100% {
    transform: translateY(-5px) scale(1.2);
  }
}

【讨论】:

  • 很好的建议。但不幸的是,悬停会导致新的box-move-anim 立即覆盖现有动画,从而导致看起来有问题的生涩活泼的效果。我正在经历更平稳的过渡。不过还是谢谢
  • 它不流畅,因为您还需要在悬停中添加过渡。你可以试试。我已经检查过了。
  • 很抱歉,不幸的是,将transition 添加到:hover 对我没有任何影响。
【解决方案2】:

考虑另一个包装器:

.box {
  width: 50%;
  animation: box-move 1s infinite alternate-reverse;
}
.box> div {
  border: solid 3px #555;
  transition: transform 300ms;
  transform-origin:top left;
}

.box:hover > div {
  transform: scale(1.2);
}

@keyframes box-move {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(-5px);
  }
}
<div class="box"><div>I'm a box. I move up and down, but I don't scale nicely when hovered like I should :(</div></div>

【讨论】:

  • 优秀的解决方案——我显然错过了transform-origin:top left,我应该在你之前的评论中使用它来纠正变换原点的问题。谢谢
【解决方案3】:

好的,感谢您的聪明而有用的建议,我设法找到了一个令人满意的解决方案。对我来说,关键是在:hover 上使用设置为forwardsanimation-direction 属性。我无法真正解释为什么会这样,但我只知道没有它就无法正常工作。

理想情况下,我仍然希望缩小(悬停)与缩小(目前只是回弹)一样平滑,但这将满足我的需要。

再次感谢。

.box {
  width: 50%;
  margin: 1em auto 0 auto;
  border: solid 3px #555;
  cursor: pointer;
  animation: box-move 1s infinite alternate-reverse;
}

.box:hover {
  animation: box-move-anim 300ms 1 forwards;
}

@keyframes box-move {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(-5px);
  }
}

@keyframes box-move-anim {
  0% {
    transform: scale(1);
  }
  100% {
    transform: scale(1.2);
  }
}
<div class="box">I'm a box that animates up and down, but I now smoothly scale when hovered :)</div>

【讨论】:

  • 对我来说,这是一个比添加另一个 <div> 包装器更好的解决方案,因为它不需要其他元素,因此更简洁。
猜你喜欢
  • 2012-08-29
  • 2017-05-19
  • 2016-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-06
  • 2018-05-03
相关资源
最近更新 更多