当鼠标已经位于 onLoad 元素上方时,带有 .toggle() 的 jQuery MouseEnter 和 MouseOut 事件会反转?

发布于 2024-08-20 15:36:30 字数 486 浏览 2 评论 0原文

我有一个下拉菜单,可以很好地使用以下代码:

$('#menu ul li').mouseenter(function(){
    $(this).children(".dropdown").toggle();
}).mouseleave(function(){
    $(this).children(".dropdown").toggle();
});

这正如您所期望的那样。问题是,如果当 $(document).ready(function() 时,鼠标已经在 $('#menu ul li')mouseenter { }) 被触发,切换会以相反的方式工作吗?

我怎样才能避免这种情况?

例如:http://screenr.com/wbd

I have a dropdown menu working great with the following code:

$('#menu ul li').mouseenter(function(){
    $(this).children(".dropdown").toggle();
}).mouseleave(function(){
    $(this).children(".dropdown").toggle();
});

This works as you would expect. The issue is that if the mouse is already mouseenter on the $('#menu ul li') when the $(document).ready(function(){ }) is fired, the toggle works the other way round?

How can I avoid this?

EG: http://screenr.com/wbd

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

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

发布评论

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

评论(3

樱花细雨 2024-08-27 15:36:30

您不想在 mouseenter 上执行 show() 并在 mouseleave 上执行 hide() 吗?

don't you want to do show() on mouseenter and hide() on mouseleave?

十年九夏 2024-08-27 15:36:30

您的鼠标事件处理程序可以传入显式布尔 showOrHide 值,而不是仅调用不带参数的 toggle 。将 true 传递给 mouseenter 例程中的 toggle(),并在 mouseleave 中传递 false 。或者按照其他答案的建议进行操作,只需使用 showhide 即可。

Instead of just calling toggle with no arguments, your mouse event handlers can pass in explicit boolean showOrHide values. Pass true to toggle() in the mouseenter routine, and false in mouseleave. Or do as suggested by the other answer and just use show and hide.

回忆追雨的时光 2024-08-27 15:36:30

当您在不移动鼠标的情况下加载文档时,在移动鼠标之前不会切换鼠标悬停操作。但我认为鼠标悬停会随着鼠标移动而切换,如果它已经在 onLoad“上方”,则发生事件。

这段代码怎么样?
不管怎样,我认为你的错误来自于 mouseout 没有被切换,因为 JS 只检查每 x 毫秒,如果你移动得太快,它就会在不调用事件的情况下离开 div。

$('#menu ul li').mouseenter(function(){
    // hide all other divs
    $(".dropdown").hide(0);
    // show the div we want
    $(this).children(".dropdown").show(0);
}).mouseleave(function(){
    $(".dropdown").hide(0);
});

如果您不关心动画,那么使用 CSS 执行此操作始终比使用 JS 更好。

你需要像这样设置它:

<div id="menu">
    <ul>
        <li>
            Menu 1
            <div class="dropdown">Sub1</div>
            <div class="dropdown">Sub2</div>
        </li>
        <li>
            Menu 2
            <div class="dropdown">Sub1</div>
            <div class="dropdown">Sub2</div>
        </li>
    </ul>
</div>

CSS:

.dropdown { display: none; position: relative; }
#menu li:hover .dropdown { display: block; }

When you load the document without moving the mouse, no mouseover action will be toggled until you move the mouse. But i think mouseover is toggled as your mouse moves, event if it's already "over" onLoad.

What about this code ?
Anyways, i think your bug comes from mouseout not being toggled because JS only checks every x ms, and if you move too fast, it gets out of the div without calling the event.

$('#menu ul li').mouseenter(function(){
    // hide all other divs
    $(".dropdown").hide(0);
    // show the div we want
    $(this).children(".dropdown").show(0);
}).mouseleave(function(){
    $(".dropdown").hide(0);
});

If you don't care about animations, doing this with CSS is always a better way then with JS.

You'll need to set it up like this :

<div id="menu">
    <ul>
        <li>
            Menu 1
            <div class="dropdown">Sub1</div>
            <div class="dropdown">Sub2</div>
        </li>
        <li>
            Menu 2
            <div class="dropdown">Sub1</div>
            <div class="dropdown">Sub2</div>
        </li>
    </ul>
</div>

CSS:

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