【发布时间】:2019-01-26 09:57:41
【问题描述】:
如何使用持续时间为 0.3 秒的从底部到顶部的过渡来更改 a:hover 的背景颜色?
<ul>
<li><a></a></li>
<li><a></a></li>
<li><a></a></li>
</ul>
这可能吗?
谢谢
【问题讨论】:
标签: css background css-transitions
如何使用持续时间为 0.3 秒的从底部到顶部的过渡来更改 a:hover 的背景颜色?
<ul>
<li><a></a></li>
<li><a></a></li>
<li><a></a></li>
</ul>
这可能吗?
谢谢
【问题讨论】:
标签: css background css-transitions
无法(通常)在 CSS 中应用过渡方向。但是,我们可以解决这个限制,方法是使用伪元素(或其他方法,例如 this example 如何使用渐变)。
通过使用伪元素,它最初的 可见 高度为 0,当链接悬停时,我们可以将高度从和到所需方向转换。出于性能原因,最好使用transform: scale,这意味着在这种情况下我们需要将transform-origin 设置为bottom center,以确保它从下到上。
这种方法可能是最通用的,适用于非纯色背景等,但确实需要伪元素或子元素。
该方法的 CSS 将是:
li {
background: red;
}
a {
position: relative;
z-index: 1;
}
a::after {
content: "";
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 100%;
transform: scaleY(0);
transform-origin: bottom center;
background: blue;
z-index: -1;
transition: transform 0.3s;
}
a:hover::after {
transform: scaleY(1);
}
【讨论】: