如果将鼠标悬停在文本下方,但在div内,动画将在中途中断,并使该元素在动画和非动画之间不断切换,从而使该元素成为一个闪烁的"动画".

@keyframes hover {
  0% {bottom: 0px; border-radius: 0%;}
  100% {bottom: 100px; border-radius: 10%;}
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
}

#i {
    transition: 0.5s ease;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: rgb(255, 100, 0);
    width: 200px;
    height: 200px;
    font-size: 50px;
    text-align: center;
    font-family: sans-serif;
    bottom: 0px;
}

#i:hover {
    position: relative;
    cursor: pointer;
    animation: hover 0.5s ease;
    bottom: 100px;
    border-radius: 10%;
}
<!DOCTYPE html>
<html lang="en">
  <head>
  </head>
  <body>
    <div id="i"><strong>Hover</strong></div>
  </body>
</html>

这是一个问题,当用户通过一个按钮太快,那么我该如何解决这个问题?

推荐答案

问题是#i向上移动(位置改变),而你的悬停位置仍然相同,没有#i.因此,它应用了#i种样式(而不是#i:hover种样式).

您可以为#i添加一个容器元素,这将帮助您为悬停行为保留容器空间.

@keyframes hover {
  0% {
    bottom: 0px;
    border-radius: 0%;
  }
  100% {
    bottom: 100px;
    border-radius: 10%;
  }
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
}

.container #i {
  transition: 0.5s ease;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgb(255, 100, 0);
  width: 200px;
  height: 200px;
  font-size: 50px;
  text-align: center;
  font-family: sans-serif;
  bottom: 0px;
}

.container:hover #i {
  position: relative;
  cursor: pointer;
  animation: hover 0.5s ease;
  bottom: 100px;
  border-radius: 10%;
}
<!DOCTYPE html>
<html lang="en">

<head>
</head>

<body>
  <div class="container">
    <div id="i"><strong>Hover</strong></div>
  </div>
</body>

</html>

只需注意一点,如果动画完成,animation支持完整的动画周期.如果你在制作动画时试图打断它,CSS不会帮助你完成它(会发生故障).

在这种情况下,可以使用transition而不是animation

transition: 0.5s ease;

如果你想用keyframes来制作一个复杂的动画,你可以考虑用JavaScript来处理它,你可以判断this document.

Html相关问答推荐

如何格式化HTML文本,使其环绕关键字

通过POST方法提交html表单时CSV文件无法通过(在使用Django的上下文中)

在css嵌套 Select 器中,尾随的`&;‘是什么意思?

如何修复与导航栏重叠的css网格?

如何收集<;p&>元素下的<;p>;子元素

窗口视图之外的下拉菜单位置

如何创建带有粘性列和标题的网格?

有没有一种方法可以很容易地将这个div从底部:0转换到顶部:0?

如何使用*ngFor将模板从父级传递到子级

在移动屏幕上显示时分支树视图的响应能力问题

按钮悬停效果不影响按钮内的图标

当浏览器宽度减小时字体大小变得太大

在HTML使用link标签导入外部CSS时出现元素link不允许使用尾斜杠错误

为什么 css position:fixed 不适用于对话框标签?

如何使用动画拆分和对齐文本块

如何使用 CSS 和 HTML 将文本放入图像中?

在 jinja 模板内的 html 表中嵌套 For 循环

在 React (NEXT) 中,当我try 进行动态补充时,我无法使用实例this来引用输入

如何在没有 JS 的情况下创建带搜索的 Select 菜单

如何使列表的第一个元素比 css 中的其他元素大?