css - CSS - 来回动画 css 设置
问题描述
如果您将鼠标悬停在我的示例中的框上,那么小框的颜色会慢慢地从红色变为黄色。但如果你停止悬停,它会立即跳回红色。
.container { position: relative; background: black; width: 200px; height: 200px; }
.subcontainer { position: absolute; bottom: 10px; width: 100%; height: 20px; background: red; animation-duration: 2s; }
.container:hover .subcontainer { background: yellow; animation-duration: 2s; animation-name: example; }
@keyframes example {
from {background-color: red;}
to {background-color: yellow;}
}
<div class="container">
<div class="subcontainer">
</div>
</div>
如何防止这种即时颜色变化?我也尝试添加animation-duration: 2s;
,.subcontainer
但它不起作用。
解决方案
你需要一个transition
定义在.subcontainer
而不是animation
.container {
position: relative;
background: black;
width: 200px; height: 200px; }
.subcontainer {
position: absolute;
bottom: 10px; width: 100%; height: 20px;
background: red;
transition: background 2s 0s }
.container:hover .subcontainer { background: yellow; }
<div class="container">
<div class="subcontainer">
</div>
</div>
推荐阅读
- flutter - 如何使变量保持不变?
- javascript - 反应不保存勾选的复选框
- python - PANDAS dataframe python: wanting to sort values by group
- angular - 角度列表包含元素但长度为 0
- html - KnockoutJS MVC 部分查看选中信息
- json - 使用 VBA (Access 2010) 下标解析 JSON 超出范围错误
- performance - QEMU 中的仿真和能耗
- java - 使用java语言在spark上应用
- php - 基本阵列构建
- python - Django 2.2 反向查找 NoReverseMatch 错误