简体   繁体   English

如何在鼠标 hover 上显示 div 元素?

[英]How to show a div element on mouse hover?

I need to show a div when a list element is hovered over, sort of like a drop-down menu.当列表元素悬停时,我需要显示一个div ,有点像下拉菜单。

I already have a way to do this with jQuery which is what I want, but the problem is that when I move mouse away from the list ( li element), the div disappears.我已经有办法用 jQuery 做到这一点,这正是我想要的,但问题是当我将鼠标从列表( li元素)移开时, div消失了。 I want to be able to move the mouse from the list element to the div and be able to interact with the elements within the div.我希望能够将鼠标从列表元素移动到 div 并能够与 div 中的元素进行交互。

This would be solved with a click function but I don't want to use click because the elements on the div will contain anchor links that when click will take users down the page, therefore you can see how a click function that shows and keep the div is not a good idea, unless I can find a way to close the div when its contents (anchor links) are clicked.这将通过单击 function 解决,但我不想使用单击,因为 div 上的元素将包含锚链接,单击时会将用户带到页面下方,因此您可以看到单击 function 如何显示并保留div 不是一个好主意,除非我能找到一种方法在单击 div 的内容(锚链接)时关闭它。

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <style>.nav-item-dropdown { position: absolute; /* other styles... */ } </style> <div class="nav-wrap"> <ul> <li id="nav-item1" class="nav-item-wrap">Services</li> <li id="nav-item2" class="nav-item-wrap">Projects</li> </ul> </div> <div class="nav-item-dropdown nav-item1-dropdown"> <.-- Drop-down nav contents for services (title and image) wrapped by anchor link goes here --> </div> <script> $(document).ready(function() { jQuery('#nav-item1').hover(function() { $('.nav-item1-dropdown');toggle(); }). jQuery('#nav-item2').hover(function() { $('.nav-item2-dropdown');toggle(); }); }); </script>

I want to be able to move the mouse from the list element ( .nav-item-wrap ) to the div ( .nav-item-dropdown ) and be able to interact with the elements within the div .我希望能够将鼠标从列表元素( .nav-item-wrap )移动到div.nav-item-dropdown )并能够与div中的元素进行交互。 I don't want the div to disappear when I move the mouse away from the list element that triggered it.当我将鼠标移离触发它的列表元素时,我不希望div消失。

.toggle() method simply toggles the visibility of elements. .toggle()方法只是切换元素的可见性。 In your code they do well what they are for.在您的代码中,他们做得很好。 In your case, instead of toggle use .show() and .hide() like below.在您的情况下,而不是切换使用.show()和 .hide( .hide()如下所示。 You need additional class to hide div when load.您需要额外class在加载时隐藏div

 $(document).ready(function() { jQuery('#nav-item1').hover(function() { $('.nav-item1-dropdown').show(); $('.nav-item2-dropdown').hide(); }); jQuery('#nav-item2').hover(function() { $('.nav-item2-dropdown').show(); $('.nav-item1-dropdown').hide(); }); });
 .nav-item-dropdown { position: absolute; /* other styles... */ }.yourClass { display: none }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="nav-wrap"> <ul> <li id="nav-item1" class="nav-item-wrap">Services</li> <li id="nav-item2" class="nav-item-wrap">Projects</li> </ul> </div> <div class="nav-item-dropdown nav-item1-dropdown yourClass"> div1 <!-- Drop-down nav contents for services (title and image) wrapped by anchor link goes here --> </div> <div class="nav-item-dropdown nav-item2-dropdown yourClass"> div2 <!-- Drop-down nav contents for services (title and image) wrapped by anchor link goes here --> </div>

Try this:尝试这个:

 $(document).ready(function(){
      jQuery('#nav-item1').mouseover(function() {
         $('.nav-item1-dropdown').show();
      }); 
      jQuery('#nav-item2').mouseover(function() {
         $('.nav-item2-dropdown').show();
      });
   });

I think you can use something like this for showing and hiding the div我认为你可以使用这样的东西来显示和隐藏 div

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> <div class="nav-wrap"> <ul> <li id="nav-item1" class="nav-item-wrap">Services</li> <li id="nav-item2" class="nav-item-wrap">Projects</li> </ul> </div> <div class="nav-item-dropdown nav-item1-dropdown" style="display:none"> <p>your action goes here</p> </div> <script> $(document).ready(function() { jQuery('.nav-wrap').hover(function() { $('.nav-item1-dropdown').show(); }); $('.nav-item1-dropdown').hide(); //use it wherever you need to hide it }); </script>

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

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