【发布时间】:2018-04-30 19:20:46
【问题描述】:
我有一个显示为块的标签。在页面加载时,它的宽度由一个 css animation 从零增加到包含 div 的某个百分比(小提琴包含一个 MWE,但是这个 div 中有多个链接,每个链接都有不同的宽度)。在悬停时,我希望它使用 CSS transition 更改颜色、更改背景颜色并扩展到 div 的 100%。颜色和背景颜色位有效,但似乎忽略了宽度过渡。
片段:
.home-bar {
text-decoration: none;
background-color: white;
color: #5e0734;
display: block;
-webkit-animation-duration: 1.5s;
-webkit-animation-timing-function: ease-out;
-webkit-animation-fill-mode: forwards;
animation-duration: 1.5s;
animation-fill-mode: forwards;
transition: color, background-color, width 0.2s linear;/*WIDTH IGNORED*/
border: 2px solid #5e0734;
overflow: hidden;
white-space: nowrap;
margin-right: 0;
margin-left: 5px;
margin-top: 0;
margin-bottom: 5px;
padding: 0;
}
.home-bar:hover {
background-color: #5e0734;
color: white;
width: 100%;/*WIDTH IGNORED*/
text-decoration: none;
}
#bar0 {
-webkit-animation-name: grow0;
animation-name: grow0;
}
@keyframes grow0 {
from {
width: 0%;
}
to {
width: 75%;
}
}
<a href="#" id="bar0" class="home-bar">LINK</a>
注意 - 我已经通过更改悬停时链接的高度对其进行了测试,并且它有效。只有宽度不起作用。也许它与页面加载时的动画有关。
【问题讨论】:
-
所以当您将链接悬停在页面加载之外时,您不想更改/动画宽度吗?我不确定我是否理解正确?
-
@MrBuggy 抱歉,不,我会说得更清楚。有五个链接,每个链接在页面加载时都会扩展到 div 的某个百分比(小于 100%)。这行得通,而且很完美。然后我想要一个 further 过渡,在悬停期间将链接的宽度扩大到更多(到 100%)。
-
为了清楚起见,请将您的整个代码放入片段中并描述会发生什么,以及应该发生什么......
-
显然动画中的
width:75%覆盖了:hover 规则中的width:100%。不知道为什么,但是您可以通过在 :hover 中定义一个新动画来覆盖它。 jsfiddle.net/MrLister/s5bopjet/2 可以吗? -
@MrLister 是的,这一定是发生了什么,但我不知道为什么。也许是因为
animation-fill-mode: forwards;?
标签: css css-transitions css-animations