3 回答
TA贡献2065条经验 获得超13个赞
您可以连接两个或更多转换,visibility这次是方便的。
div {
border: 1px solid #eee;
}
div > ul {
visibility: hidden;
opacity: 0;
transition: visibility 0s, opacity 0.5s linear;
}
div:hover > ul {
visibility: visible;
opacity: 1;
}
<div>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
不要忘记transition
属性的供应商前缀)
更多细节在本文中
TA贡献1936条经验 获得超6个赞
在本文发表时,如果您尝试更改display
属性,所有主流浏览器都会禁用CSS过渡,但CSS动画仍然可以正常工作,因此我们可以将它们用作解决方法。
示例代码: - (您可以相应地将它应用于您的菜单) 演示
将以下CSS添加到样式表: -
@-webkit-keyframes fadeIn { from { opacity: 0; } to { opacity: 1; }} @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; }}
然后将fadeIn
动画应用于父hover上的子项: - (当然设置display: block
)
.parent:hover .child { display: block; -webkit-animation: fadeIn 1s; animation: fadeIn 1s;}
更新2019 - 也支持淡出的方法:
(有些JS需要)
// We need to keep track of faded in elements so we can apply fade out later in CSS
document.addEventListener('animationstart', function (e) {
if (e.animationName === 'fade-in') {
e.target.classList.add('did-fade-in');
}
});
document.addEventListener('animationend', function (e) {
if (e.animationName === 'fade-out') {
e.target.classList.remove('did-fade-in');
}
});
div {
border: 5px solid;
padding: 10px;
}
div:hover {
border-color: red;
}
.parent .child {
display: none;
}
.parent:hover .child {
display: block;
animation: fade-in 1s;
}
.parent:not(:hover) .child.did-fade-in {
display: block;
animation: fade-out 1s;
}
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes fade-out {
from {
opacity: 1;
}
to {
opacity: 0;
}
}
<div class="parent">
Parent
<div class="child">
Child
</div>
</div>
- 3 回答
- 0 关注
- 760 浏览
相关问题推荐
添加回答
举报