简体   繁体   English

jQuery:悬停在子菜单项上方时,保持子菜单打开

[英]jQuery: Keep submenu open while hovering over submenu items

This has been asked a lot. 这已经被问了很多。 But I can't seem to find a solution that works. 但是我似乎找不到有效的解决方案。 I have a jQuery code that opens my submenu on hover, but it vanishes after I try to go over the submenu items. 我有一个jQuery代码,可以在悬停时打开子菜单,但是在尝试浏览子菜单项后它消失了。 How can I fix that? 我该如何解决?

Here's my code: 这是我的代码:

 $('.hover').hover( function() { $('.drop-box').show(); }, function() { $('.drop-box').hide(); }) }); 
 .drop-box { position: absolute; width: 100%; height: 60px; background: darkgreen; top: 60px; left: 0; display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="hover">hover</div> <div class="drop-box"> <div class="container row"> <div class="col-md-4"> <p class="mb-0">Text</p> </div> <div class="col-md-4"> <p class="mb-0">Text</p> </div> <div class="col-md-4"> <p class="mb-0">Text</p> </div> </div> </div> </div> 

Just place .drop-box and all its contents inside .hover . 只需将.drop-box及其所有内容放入.hover Plus, you need to remove the top property to prevent the gap between the text and the .drop-box . 另外,您需要删除top属性以防止文本和.drop-box之间出现间隙。

 $('.hover').hover( function () { $('.drop-box').show(); }, function () { $('.drop-box').hide(); } ); 
 .drop-box { position: absolute; width: 100%; height: 60px; background: darkgreen; // top: 60px; left: 0; display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="hover">hover<div class="drop-box"> <div class="container row"> <div class="col-md-4"> <p class="mb-0">Text</p> </div> <div class="col-md-4"> <p class="mb-0">Text</p> </div> <div class="col-md-4"> <p class="mb-0">Text</p> </div> </div> </div> </div> 

UPDATE: 更新:

If you can't move .drop-box into .hover , you can wrap everything with a div and add the hover event using CSS: 如果您无法将.drop-box移至.hover ,则可以使用div包装所有内容,并使用CSS添加hover事件:

 .drop-box { position: absolute; width: 100%; height: 60px; background: darkgreen; // top: 60px; left: 0; display: none; } .everything:hover .drop-box { display: block; } 
 <div class="everything"> <div class="hover">hover</div> <div class="drop-box"> <div class="container row"> <div class="col-md-4"> <p class="mb-0">Text</p> </div> <div class="col-md-4"> <p class="mb-0">Text</p> </div> <div class="col-md-4"> <p class="mb-0">Text</p> </div> </div> </div> </div> 

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

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