简体   繁体   English

悬停效果仅对父div

[英]Hover effect only on parent div

I have a sidebar that expands on hover, right now im hiding the text with opacity and when i hover on the hidden spans it triggers the effect. 我有一个侧边栏,它会在悬停时扩展,现在我会隐藏不透明的文本,当我将鼠标悬停在隐藏的跨度上时会触发效果。

how can i solve this ? 我该如何解决? or maybe there is a better way to create this kind of effect that im looking for 或者也许有更好的方法来创建这种效果,我正在寻找

Thanks in advance. 提前致谢。

https://jsfiddle.net/aq9Laaew/2618/ https://jsfiddle.net/aq9Laaew/2618/

 #sidebar { user-select: none; position: fixed; width: 55px; height: 100vh; background: #2c2c2c; transition: 0.5s; } #sidebar:hover { width: 250px; } #sidebar:hover .sidebar-item span { opacity: 1; } .sidebar-item { padding: 10px 10px; border-left: 5px solid transparent; } .sidebar-item span { opacity: 0; transition: 0.5s; } 
 <div id="sidebar"> <div class="sidebar-wrapper"> <div class="sidenav-menu d-flex flex-column flex-grow"> <div class="sidebar-item"> <a class="d-flex" href="/"> <i class="fa fa-lg fa-home" style="margin-left: 1px; margin-right: 20px"></i> <span>Home</span> </a> </div> <div class="sidebar-item"> <a class="d-flex" href="/profile"> <i class="fa fa-lg fa-user-o" style="margin-left: 1px; margin-right: 22px"></i> <span>Profile</span> </a> </div> </div> </div> </div> 

You changed the opacity of your elements but they're still in the document and so they're triggering the effect. 您更改了元素的不透明度,但它们仍在文档中,因此触发了效果。

By using visibility: hidden / visible , hovering the labels won't trigger the effect : 通过使用visibility: hidden / visible ,将标签悬停不会触发效果:

.sidebar-item span {
    opacity:0;
    transition: 0.5s;
    visibility : hidden
}       
#sidebar:hover .sidebar-item span{
    opacity:1;
    visibility : visible
}

Your updated JSFiddle 您更新的JSFiddle

我认为您可以通过设置overflow: hidden来解决错误overflow: hidden#sidebar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM