html - 悬停图像上的 CSS 动画效果不起作用

标签 html css css-animations

我在 CSS 中创建了一个动画效果,它在文本上效果很好。我也想在用作图标的 PNG 图像上使用它,但它不起作用。

.logo-box {
  position: absolute;
  top: 40px;
  left: 40px;
}

.logo {
  height: 35px;
}

.logo:hover {
  animation: moveInRight 1.5s ease-out;
}

@keyframes moveInRight {
  0% {
    opacity: 0;
    transform: translateX(100px);
  }
  80% {
    transform: translateX(-10px);
  }
  100% {
    opacity: 1;
    transform: translateX(0);
  }
}
<div class="logo-box">
  <img class="logo" src="img/logo-white.png">
</div>

最佳答案

不是选择悬停的 .logo 本身(移动它的位置),而是定位不移动的包装器 (.logo-box),然后定位.logo 用于动画。而不是

.logo:hover {
  animation: moveInRight 1.5s ease-out;
}

使用

.logo-box:hover .logo {
  animation: moveInRight 1.5s ease-out;
}

示例:

.logo-box {
  position: absolute;
  top: 40px;
  left: 40px;
}

.logo {
  height: 35px;
}

.logo-box:hover .logo {
  animation: moveInRight 1.5s ease-out;
}

@keyframes moveInRight {
  0% {
    opacity: 0;
    transform: translateX(100px);
  }
  80% {
    transform: translateX(-10px);
  }
  100% {
    opacity: 1;
    transform: translateX(0);
  }
}
<div class="logo-box">
  <img class="logo" src="img/logo-white.png">
</div>

关于html - 悬停图像上的 CSS 动画效果不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57493115/

相关文章:

html - 第 n 个 child 使用配方奶粉

jquery - 为什么 dragleave 事件意外触发?

css - svg 动画不遵循定义的路径,移动不规律

html - 如何创建倾斜的多行段落(HTML CSS)?

CSS3 过渡出自定义动画

html - 为什么 Safari 忽略最小宽度和文本对齐?

html - 如何对齐 Bootstrap 表单的输入字段和标签?

javascript - Bootstrap 移动下拉父链接问题

html - Css animate/show div after other

css - 在css3动画中添加延迟时间