JQuery:检测鼠标何时离开多个元素

发布于 2024-10-13 18:44:57 字数 1360 浏览 5 评论 0原文

我有一个由 div 组成的菜单,其中一个 div 有一个 JQuery mouseenter 函数,可以向下滑动下拉菜单:

$(".dropdownLauncher").mouseenter(function() {
  $(".dropdown").slideDown("slow");
});

我还有一个函数可以将下拉菜单向后滑动鼠标离开下拉菜单:

$(".dropdown").mouseleave(function() {
  $(".dropdown").slideUp("slow");
});

这很好,除了当用户将鼠标移到启动器上,然后再次移出,而不经过下拉菜单,菜单保持在下方。

是否可以检查鼠标是否位于任一 div 中?

编辑 1:标记:

<div class="menu">
                <div class="menuItem selectedItem">Home</div>
                <div class="menuItem unselectedItem leftBorder dropdownLauncher">About <img src="Arrow.gif"></div>
                <div class="dropdown">
                    <div class="menuItem unselectedItem dropdownItem topBorder">Beep</div>
                    <div class="menuItem unselectedItem dropdownItem topBorder">Beep</div>
                    <div class="menuItem unselectedItem dropdownItem topBorder">Beep</div>
                </div>
                <div class="menuItem unselectedItem leftBorder">Visiting</div>
                <div class="menuItem unselectedItem leftBorder">Newsletters</div>
                <div class="menuItem unselectedItem leftBorder">Ecology</div>
            </div>

I have a menu made of divs, and one of the divs has a JQuery mouseenter function that slides down a dropdown:

$(".dropdownLauncher").mouseenter(function() {
  $(".dropdown").slideDown("slow");
});

I also have a function that slides the dropdown back up of the mouse leaves the dropdown:

$(".dropdown").mouseleave(function() {
  $(".dropdown").slideUp("slow");
});

This would be fine, except that when the user moves the mouse over the launcher, than out again, without going through the dropdown, the menu stays down.

Is it possible to check if the mouse is in either of the divs?

EDIT 1: Markup:

<div class="menu">
                <div class="menuItem selectedItem">Home</div>
                <div class="menuItem unselectedItem leftBorder dropdownLauncher">About <img src="Arrow.gif"></div>
                <div class="dropdown">
                    <div class="menuItem unselectedItem dropdownItem topBorder">Beep</div>
                    <div class="menuItem unselectedItem dropdownItem topBorder">Beep</div>
                    <div class="menuItem unselectedItem dropdownItem topBorder">Beep</div>
                </div>
                <div class="menuItem unselectedItem leftBorder">Visiting</div>
                <div class="menuItem unselectedItem leftBorder">Newsletters</div>
                <div class="menuItem unselectedItem leftBorder">Ecology</div>
            </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤芳又自赏 2024-10-20 18:44:57

好吧,这种方式有很多问题......打开任何菜单(例如浏览器菜单),然后移动鼠标 - 它不是隐藏的。这是正常的方式。当您单击某处时,菜单将被隐藏(因此您可以将单击事件处理程序附加到文档)。
即使您进入子菜单,当鼠标悬停在子菜单上时,它也会显示,而当您将鼠标移动到另一个菜单项时,它会隐藏;但是当您将鼠标移出菜单时,子菜单不会隐藏。

如果您仍然想实现您的想法,您将需要实现复杂的系统来控制用户的操作。将 mouseleave 处理程序添加到 dropdownLauncher,注册用户离开此项目并触发下拉菜单隐藏。现在在隐藏下拉菜单的函数中检查鼠标是否位于下拉菜单上 - 取消隐藏;否则隐藏它。不要忘记清理该变量的状态(“left dropdownLauncher”)。例如,您可以在用户离开启动器后 1 秒内执行隐藏下拉菜单。

另请注意,当用户快速移动鼠标(只是猛烈地移动)时,浏览器没有时间触发某些事件(我在鼠标移动时遇到了问题)...请务必测试这种情况。

好吧,没有附加代码,但我希望你能明白。

更新:可能您对这个问题不再感兴趣(答案来得太晚了,所以我理解;),但我发现了非常有趣的解决方案,易于支持:

Click Examples Page

打开该页面,有菜单。据我所知,这就是您想要实现的(我的意思是菜单导航的相同行为)。在查看 JavaScript 代码后,我发现它非常小,而且与该菜单无关......所以我很清楚,他们使用纯 HTML+CSS 来执行它。只需看一下小 css 文件,并且看一下菜单 html 标记:

<div id="rootMenu" class="menustyle">
  <ul class="menubar">
      <li class="topitem">
        <a class="selected" href="_link_"><img border="0" src="/click-examples/assets/images/home.png" alt=" Home" class="link"> Home</a>
        <ul class="submenu">
           <li>
              <a title="BorderPage Java source" target="_blank" href="_link_">BorderPage Class</a>
           </li>
           <li>
              <a title="Page border HTML template" target="_blank" href="_link_">Border Template HTML</a>
            </li>
            ....
        </ul>
      </li>
      ....
   </ul>
   ....
</div>

其他一切都是由 css 完成的(特别是通过 :hover 伪类)。我喜欢这种方法。所以现在你至少有选择了;)

Well, this way is quite buggy... open any menu (e.g. your browser menu), and move mouse - it is not hidden. This is normal way. When you click somewhere then menu is hidden (so you can attach click event handler to document).
Even when you go to submenu, it is shown when mouse is over it, and is hidden when you move mouse to another menu item; but when you move mouse out of menu, submenu is not hidden.

If you still wanna implement your idea, you will need to implement complex system to control users actions. add mouseleave handler to dropdownLauncher, register that user left this item and trigger dropdown menu hiding. Now in function which hides dropdown menu check if mouse is over dropdown menu - cancel hiding; otherwise hide it. Do not forget to clean state of that variable ("left dropdownLauncher"). E.g. you can execute dropdown menu hiding in 1 second after user left Launcher.

Also be careful, when user moves mouse quite fast (just jerks), browser has no time to trigger some events (I had problems with mousemove)... Be sure that you test this case.

Well, no code attached, but I hope that you get the idea.

UPDATE: Probably you are not interested in this question anymore (answer comes so late, so I understand ;), but I've found very interesting solution, easy to support:

Click Examples Page

open that page, there is menu. As I got it, this is what you want to implement (I mean the same behavior of menu navigation). After looking at JavaScript code I noticed that it is very small, and even more, it is not related to that menu... So it was clear to me, they use pure HTML+CSS to perform it. Just take a look at small css file, and also take a look and menu html markup:

<div id="rootMenu" class="menustyle">
  <ul class="menubar">
      <li class="topitem">
        <a class="selected" href="_link_"><img border="0" src="/click-examples/assets/images/home.png" alt=" Home" class="link"> Home</a>
        <ul class="submenu">
           <li>
              <a title="BorderPage Java source" target="_blank" href="_link_">BorderPage Class</a>
           </li>
           <li>
              <a title="Page border HTML template" target="_blank" href="_link_">Border Template HTML</a>
            </li>
            ....
        </ul>
      </li>
      ....
   </ul>
   ....
</div>

everything else is done by css (specifically by :hover pseudo-class). I liked that approach. So now you have alternative, at least ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文