Mar*_*vin 11 css css3 css-transitions
我回答了一个关于如何在悬停子元素时启动动画然后保留应用的样式直到取消悬停父元素的问题.但是,我在我提出的解决方案中发现了一种我无法解释并且我想理解的行为.阅读规范的相关部分对我没有帮助.
这是一个显示预期行为的最小示例.它有效,但由于某些原因,背后有评论的属性必须不同.否则(例如,两者都具有10px值),父母的悬停不会对孩子的宽度做任何事情.JSFiddle.
.parent {
border: 1px solid orange;
padding: 20px;
width: 400px;
}
.parent .child {
display: inline-block;
height: 40px;
background: blue;
transition: width 0.5s ease 600s;
width: 10px; /* why does this value has to be different ... */
/* Hint:
If you hover the child until it reaches the 100px, then hover the
parent without leaving the parent and keeping that hover for the
transition-delay (600s) the width will become 10px as defined here.
And removing this width property here won't make the transition work
at all. */
}
.parent .child:hover {
transition-delay: 0s;
width: 100px;
}
.parent:not(:hover) .child {
transition: width 0.5s ease 0s;
width: 11px; /* ... from this value? */
/* Hint:
This is used as some kind of interruption of the 600s
transition-delay in order to achieve the parent un-hover effect.
I would like to set the width to 10px here as well but this will
result in having no effect on the width of the enlarged child when
un-hovering the parent. */
}Run Code Online (Sandbox Code Playgroud)
<div class="parent">
<div class="child">
</div>
</div>Run Code Online (Sandbox Code Playgroud)
相关浏览器是Firefox和Chrome.在Firefox中,以下工作:
.parent .child {
/* ... */
transition: width 0.5s ease 600s;
width: calc(10px);
}
.parent:not(:hover) .child {
transition: width 0.5s ease 0s;
width: 10px;
}
Run Code Online (Sandbox Code Playgroud)
为什么宽度属性的值必须不同才能使非悬停效果像预期的那样工作?
对不起 - 我很想念yopur问题中发生了什么.
我做了一个带有简化案例的新代码片段:
#a, #b {
width: 200px;
height: 100px;
border: solid 1px black;
display: inline-block;
background-color: lightgreen;
margin-top: 50px;
}
#a {
margin-right: -5px;
}
#container {
width: 400px;
height: 50px;
border: solid 1px black;
margin: 0px;
}
#child {
width: 0px;
height: 50px;
position: absolute;
background-color: blue;
}
#child {
transition: width 0.5s;
width: 400px;
}
#a:hover ~ #container #child {
transition: width 10s;
width: 0px;
}
#b:hover ~ #container #child {
transition: width 0.5s;
width: 0px;
}Run Code Online (Sandbox Code Playgroud)
<div id="a">A</div>
<div id="b">B</div>
<div id="container">
<div id="child"></div>
</div>Run Code Online (Sandbox Code Playgroud)
将鼠标悬停在A上,然后(在转换结束之前)悬停B.您将看到相同的行为:转换继续保持不变.
原因是当悬停a时,宽度(作为元素的属性)是0px.(不是正在转换的计算宽度).因此,当您将鼠标悬停在B上时,0px的新样式不会触发属性更改,并且hende将不会启动新的转换.
规范的关键部分是这一个:(强调我的)
为了满足这种期望,当一个元素的属性开始转换时(此后,新的转换)具有当前正在运行的转换,其转换调整后的起始值与新转换的结束值相同(此后,实现必须取消旧转换[...]并按如下方式调整新转换(在遵循计算合并持续时间,开始时间和结束时间的规则之前):[...]
因此,当新宽度与旧宽度相同时,并不是真正的非悬停没有效果,因为浏览器正在逆转正在运行的转换,效果非常慢(600s).
为了证明这一点,我设置了一个片段,其中最后一条规则的宽度相同(10px).延迟设置为10秒.
将孩子悬停,然后将其移离父母.您将看到10秒后,子宽度被修改.
.parent {
border: 1px solid orange;
padding: 20px;
width: 400px;
}
.parent .child {
display: inline-block;
height: 40px;
background: blue;
transition: width 0.5s ease 10s;
width: 10px;
}
.parent .child:hover {
transition-delay: 0s;
width: 100px;
}
.parent:not(:hover) .child {
transition: width 0.5s ease 0s;
width: 10px;
Run Code Online (Sandbox Code Playgroud)
<div class="parent">
<div class="child">
</div>
</div>Run Code Online (Sandbox Code Playgroud)