悬停时从底部到顶部更改背景 [英] Change background from bottom to top on hover
问题描述
如何在 a:hover
上改变背景颜色,使用从底部到顶部的过渡时间为0.3秒?
How I can change the background-color on a:hover
using a transition from the bottom to the top with a duration of 0.3s?
<ul>
<li><a></a></li>
<li><a></a></li>
<li><a></a></li>
</ul>
有可能吗?
/ p>
Thanks
推荐答案
没有办法(一般地)在CSS中应用转换方向。但是,我们可以通过使用伪元素(或其他方法,例如
There is no way to (generically) apply a transition direction in CSS. However, we can work around that limitation by using a pseudo element (or other method like how this example uses gradients).
通过使用初始具有可见高度为0的伪元素,当链接被悬停时将高度从期望方向过渡到期望方向。由于性能原因,最好使用 transform:scale
,这意味着我们需要将 transform-origin
设置为底部中心
在这种情况下,以确保它从底部到顶部。
By using a pseudo element, which initially has a visible height of 0, we can transition the height from and to a desired direction when the link is hovered. It's best to use transform: scale
for performance reasons, which means that we need to set our transform-origin
to bottom center
in this case to make sure it goes from bottom to top.
这种方法可能是最常见的,使用非实体背景等,但确实需要一个伪元素。
This approach is probably the most general, working with non-solid backgrounds and such, but does require a pseudo element.
这种方法的CSS将是:
The CSS for that approach would be:
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);
}
这篇关于悬停时从底部到顶部更改背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!