[英]How to trigger div visibility when hovering over list item in CSS?
I'd like to hide a div using the css property visibility: none
when a user hovers over a link wrapped in a list item. 我想使用css属性
visibility: none
隐藏div visibility: none
当用户将鼠标悬停在列表项中包装的链接上时, visibility: none
。
HTML 的HTML
<div id="rightnav" class="navbar-header pull-right">
<ul class="nav navbar-nav">
<li><a id="home" href="." data-pjax="content">Works</a></li>
<li><a id="about" href="studio.php" data-pjax="content">Studio</a></li>
<li><a id="contact" href="formspree.php" data-pjax="content">Contact</a></li>
<div id="bar1"></div>
<div id="bar2"></div>
<div id="bar3"></div>
</ul>
</div>
Is there a way to select the divs with id 'bar_*' when a user hovers over one of the list items (#home, #about, #contact) purely using CSS? 当用户仅使用CSS将鼠标悬停在列表项之一(#home,#about,#contact)上时,有没有办法选择ID为“ bar_ *”的div? I'm not opposed to using jQuery/Javascript, just trying to figure out the simplest way forward & found this surprisingly tricky.
我不反对使用jQuery / Javascript,只是试图找出最简单的前进方式并发现这出奇的棘手。
This is quite simple with css try this fiddle link : https://jsfiddle.net/xfz6x9wt/ 使用CSS这非常简单,请尝试以下小提琴链接 : https : //jsfiddle.net/xfz6x9wt/
ul{
height:30px;
}
ul li a {
color:green}
ul li a:hover {
color:red;
}
ul li a:hover + div.hide{
color:red;
visibility:hidden;
}
<ul><li><a href="#">hii</a>
<div class="hide"> hide</div></li>
</ul>
You may use 您可以使用
Array.from(document.querySelectorAll('[id=^bar]')).forEach(x => x.style.visibility = 'hidden')
to select all elements with id starting with bar. 选择所有以bar开头的id的元素。
You can use sibling selector coupled with hover. 您可以将兄弟选择器与悬停一起使用。 Hope this is what you are looking for:
希望这是您想要的:
#rightnav div{ background: red; display: block; } #rightnav #bar1{ background: red; } #rightnav #bar2{ background: blue; } #rightnav #bar3{ background: green; } li:nth-child(1):hover ~ div:nth-of-type(1){ display: none !important; } li:nth-child(2):hover ~ div:nth-of-type(2){ display: none !important; } li:nth-child(3):hover ~ div:nth-of-type(3){ display: none !important; }
<div id="rightnav" class="navbar-header pull-right"> <ul class="nav navbar-nav"> <li><a id="home" href="." data-pjax="content">Works</a></li> <li><a id="about" href="studio.php" data-pjax="content">Studio</a></li> <li><a id="contact" href="formspree.php" data-pjax="content">Contact</a></li> <div id="bar1">Home</div> <div id="bar2">About</div> <div id="bar3">Contact</div> </ul> </div>
Try this: 尝试这个:
For simplicity, you can restructure your html like this: 为简单起见,您可以像这样重组HTML:
<div id="rightnav" class="navbar-header pull-right">
<ul class="nav navbar-nav">
<li>
<a id="home" href="." data-pjax="content">Works</a>
<div class="nav-content">Item-1</div>
</li>
<li>
<a id="about" href="studio.php" data-pjax="content">Studio</a>
<div class="nav-content">Item-2</div>
</li>
<li>
<a id="contact" href="formspree.php" data-pjax="content">Contact</a>
<div class="nav-content">Item-3</div>
</li>
</ul>
</div>
CSS: CSS:
.navbar-nav li:hover .nav-content {
visibility: hidden;
}
Ref: http://jsbin.com/xekojel/edit?html,css,output
参考: http : //jsbin.com/xekojel/edit?html,css,output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.