简体   繁体   English

如何在 hover 上显示特定元素?

[英]How do you display a particular element on hover?

I'm trying to make a navigation bar that displays a particular element when hovered and clicked.我正在尝试制作一个导航栏,在悬停并单击时显示特定元素。 I'd also like the other elements to remain hidden.我还希望其他元素保持隐藏。 I tried doing a few codes but it won't work even if I hovered it already.我尝试做一些代码,但即使我已经将它悬停它也不会工作。

 .yearly { display: none; }.evento { display: none; }.year a:hover+.yearly { display: inline-block; background-color: #aacde2; }.event a:hover+.evento { display: inline-block; background-color: #aacde2; font-size: 50px; }
 <div class="listo"> <ul> <li class="year"><a href="#">Yearly Donations</a></li> <li class="event"><a href="#">Events</a></li> </ul> </div> <div class="content"> <div class="yearly"> <img src="../images/pic1.jpg" alt="Photo" width="300px"> </div> <div class="evento"> <p>trial</p> </div> </div>

You can achieve this using Jquery.您可以使用 Jquery 实现此目的。

 $(".year a").on({ mouseover: function(e){ $(".yearly").show(); }, mouseout: function(e) { $(".yearly").hide(); } }); $(".event a").on({ mouseover: function(e){ $(".evento").show(); }, mouseout: function(e) { $(".evento").hide(); } });
 .yearly { display: none; }.evento { display: none; }.year a:hover +.yearly { display: inline-block; background-color: #aacde2; }.event a:hover +.evento { display: inline-block; background-color: #aacde2; font-size: 50px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="listo"> <ul> <li class="year"><a href="#">Yearly Donations</a></li> <li class="event"><a href="#">Events</a></li> </ul></div> <div class="content"> <div class="yearly"> <img src="../images/pic1.jpg" alt="Photo" width="300px"> </div> <div class="evento"> <p>trial</p> </div> </div>

You could call a Javascript function on hover.您可以在 hover 上致电 Javascript function。 I'm sure there are better ways, but this works for example:我敢肯定有更好的方法,但这适用于例如:

 function displayYearly() { document.getElementById("yearly").style.display = "inline-block"; } function hideYearly() { document.getElementById("yearly").style.display = "none";; } function displayEvento() { document.getElementById("evento").style.display = "inline-block"; } function hideEvento() { document.getElementById("evento").style.display = "none"; }
 #yearly { display: none; } #evento { display: none; }
 <div class="listo"> <ul> <li class="year" onmouseover="displayYearly()" onmouseout="hideYearly()"> <a href="#">Yearly Donations</a> </li> <li class="event" onmouseover="displayEvento()" onmouseout="hideEvento()"> <a href="#">Events</a> </li> </ul> </div> <div class="content"> <div id="yearly"> <img src="https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon@2.png" alt="Photo" width="100px"> </div> <div id="evento"> <p>trial</p> </div> </div>

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

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