简体   繁体   English

为什么CSS Hover行为不稳定?

[英]Why is CSS Hover behaviour flaky?

I want to create a button (using only css and html) that reveals another button beneath it on hover by rotating on it's lowermost axis. 我想创建一个按钮(仅使用css和html),通过在其最低轴上旋转来显示悬停在其下方的另一个按钮。

I've been mostly successful: http://codepen.io/machinarius/pen/BdtCb 我基本上都成功了: http : //codepen.io/machinarius/pen/BdtCb

But as you can see on my pen the hover behaviour is flaky at best, it resets the animation on any movement of the cursor. 但是,正如您在笔上所看到的那样,悬停行为充其量是片状的,它会在光标的任何移动上重置动画。 Why id that happening? 为什么会发生这种情况? Isn't -webkit-animation-fill-mode: both; -webkit-animation-fill-mode: both;不是-webkit-animation-fill-mode: both; supposed to reverse the animation once the selector goes off? 应该在选择器关闭后反转动画吗?

The problem is that you're applying your animation on the :hover pseudo-class. 问题是您正在将动画应用于:hover伪类。 Once the animation happens, you're no longer hovering, and so the animation resets. 动画发生后,您将不再悬停,因此动画将重置。 Try wrapping a container class around your animation element, and applying your animation trigger to the container's :hover , as in the example on https://developer.mozilla.org/en-US/docs/Web/CSS/animation-fill-mode . 尝试将容器类包装在动画元素周围,然后将动画触发器应用于容器的:hover ,如https://developer.mozilla.org/en-US/docs/Web/CSS/animation-fill-模式

There seem to be two parts to this question: 这个问题似乎有两个部分:

Why is the hovering flaky? 为什么悬停片状?

Like Palpatim said, as soon as the unfold-button is hovered over, it jumps away, so you'll need to have an unmoving element that will catch your hovers without un-hovering itself. 就像Palpatim所说的那样,只要将unfold-button悬停在上方,它就会跳开,因此您需要一个不会移动的元素来抓住您的悬停。 So let's add a div that will do this: 因此,让我们添加一个将执行此操作的div:

<div class="container">
  <div class="unfold-button orange">
    Hello World
  </div>
</div>

Likewise, let's update the CSS selector accordingly: 同样,让我们​​相应地更新CSS选择器:

.container:hover .unfold-button {

Now if you put that in your HTML, you'll see that the hovering is no longer flaky. 现在,如果将其放在HTML中,您将看到悬停不再不稳定。 However, as you described, it still isn't animating back into place. 但是,正如您所描述的,它仍然没有恢复原状。 This brings us to our second question: 这带给我们第二个问题:

Why is the animation not reversing? 为什么动画不反转?

Actually, animation-fill-mode does not mean that the animation will reverse back when the animation is no longer assigned; 实际上, animation-fill-mode并不意味着当不再分配动画时,动画将向后退。 it only determines what attributes "fill out" before and after the animation occurs. 它仅确定在动画发生之前和之后“填充”哪些属性。 If you remove the line defining animation-fill-mode , you'll see that the only difference is that, without it, the animation reverts after completing. 如果删除定义animation-fill-mode ,您将看到唯一的区别是,没有它,动画将在完成后恢复。

Also, elements have no memory of the animation values that they used to have, so as soon as an element's animation attribute changes, the element immediately "pops" into what it is assigned to be with no influence from any previous values of animation . 而且,元素不具有它们过去拥有的animation值的存储,因此,一旦元素的animation属性更改,该元素就会立即“弹出”到其指定的内容中,而不受任何先前的animation值影响。

As a result, what's actually happening with your CSS is that, when the unfold-button is hovered over, it is handed the unfold animation and plays it (like it should), but when it is un-hovered, it suddenly has no animation assigned, so having "forgotten" about the animation, it just snaps back to what it was originally assigned to be. 结果,CSS实际发生的情况是, unfold-button鼠标悬停在unfold-button上时,将其交给unfold动画并播放(就像应该的那样),但是当将其悬停时,突然没有动画分配,因此“忘记”了动画,它只是恢复到最初分配的位置。

Considering that the unfold animation is one simple motion, I would recommend expressing it instead as a transition: 考虑到unfold动画是一个简单的动作,我建议将其表示为过渡:

.unfold-button {
  /* ... */
  border-style: none;
  box-sizing: border-box;

  transform-origin: 50% 100%;
  -webkit-transform-origin: 50% 100%;

  -webkit-transition: 0.5s;

  transform: rotateX(0deg);
  -webkit-transform: rotateX(0deg);
}

.container:hover .unfold-button {
  -transform: rotateX(180deg);
  -webkit-transform: rotateX(180deg);
}

Note how the transition attribute is maintained throughout both the hovered and non-hovered states. 请注意如何在transition状态和非transition下保持transition属性。 Like with animation , no animation results from it without its immediate presence. animation一样,没有动画就不会产生动画。

And there you have it! 在那里,您拥有了!

If the HTML and CSS look like what I have sitting in front of me right now, all should be good. 如果HTML和CSS看起来像我现在坐在我眼前的样子,那么一切都应该很好。

There's a little bit more information about reversing a CSS animation on hover-out here: 这里有一些关于悬停时反转CSS动画的更多信息:
How to make CSS Animation reverse on hover-out? 悬停时如何使CSS动画反转?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM