当鼠标离开悬停 div 控制菜单时保留下拉菜单的显示

发布于 2024-12-20 11:48:16 字数 454 浏览 4 评论 0原文

我有一个简单的下拉菜单,使用以下脚本进行控制:

$(document).ready(function () {
    $('img.menu_class').hover(function () {
    $('.the_menu').slideToggle('medium');
    });
});

当鼠标离开“img.menu_class”div时,我无法弄清楚如何继续显示显示的div,即“.the_menu”但保留在“.the_menu”div 中。当鼠标位于这两个 div 中时,需要保留它。

您可以在此处查看当前状态的菜单。只需将鼠标悬停在主导航栏上(目前只是一个图像)即可查看下拉菜单。

有人可以帮忙解决这个问题吗?

谢谢,

尼克

I have a simple drop-down menu that is controlled using the following script:

$(document).ready(function () {
    $('img.menu_class').hover(function () {
    $('.the_menu').slideToggle('medium');
    });
});

I can't work out how to continue to display the div that is revealed, i.e. ".the_menu", when the mouse leaves the 'img.menu_class' div but stays in the ".the_menu" div. It needs to be preserved whilst the mouse is in either of these divs.

You can see the menu in its current state here. Just hover on the main nav bar (it's just an image at the moment) to see the drop-down menu.

Could someone help with this?

Thanks,

Nick

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

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

发布评论

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

评论(3

已下线请稍等 2024-12-27 11:48:16

您可以切换为 mouseenter()mouseleave(),而不是 hover() 并稍微更改标记。

示例:

HTML:

<div class="menu_class">
  <img src="http://salliannputman.com/images/nav.png">
  <div class="submenu" style="display: none">
    <img src="http://salliannputman.com/images/nav3.png" />
  </div>     
</div>

JS:

$('img.menu_class').mouseenter(function() {
  $('img.submenu').slideDown('medium');
});
$('img.menu_class').mouseleave(function() {
  $('img.submenu').slideUp('medium');
});

此处 是工作代码。

Instead of hover() you can switch to mouseenter() and mouseleave() and change the markup a bit.

Example:

HTML:

<div class="menu_class">
  <img src="http://salliannputman.com/images/nav.png">
  <div class="submenu" style="display: none">
    <img src="http://salliannputman.com/images/nav3.png" />
  </div>     
</div>

JS:

$('img.menu_class').mouseenter(function() {
  $('img.submenu').slideDown('medium');
});
$('img.menu_class').mouseleave(function() {
  $('img.submenu').slideUp('medium');
});

HERE is the working code.

执着的年纪 2024-12-27 11:48:16

您可以使用 setTimeout 来创建隐藏菜单的延迟:

$(function () {
    var myTimer,
        myDelay = 100;

    $('img.menu_class, .the_menu').hover(function () { 

        //when either a `img.menu_class` or a `.the_menu` element is hovered over, clear the timeout to hide the menu and slide the menu into view if it aleady isn't
        clearTimeout(myTimer);
        $('.the_menu').stop(true, true).slideDown('medium');
    },
    function () {

        //when either a `img.menu_class` or a `.the_menu` element is hovered out, set a timeout to hide the menu
        myTimer = setTimeout(function () {$('.the_menu').stop(true, true).slideUp('medium');}, myDelay);
    });
}); 

这是一个演示:http ://jsfiddle.net/7vrGu/2/

You can use setTimeout to create a delay in hiding the menu:

$(function () {
    var myTimer,
        myDelay = 100;

    $('img.menu_class, .the_menu').hover(function () { 

        //when either a `img.menu_class` or a `.the_menu` element is hovered over, clear the timeout to hide the menu and slide the menu into view if it aleady isn't
        clearTimeout(myTimer);
        $('.the_menu').stop(true, true).slideDown('medium');
    },
    function () {

        //when either a `img.menu_class` or a `.the_menu` element is hovered out, set a timeout to hide the menu
        myTimer = setTimeout(function () {$('.the_menu').stop(true, true).slideUp('medium');}, myDelay);
    });
}); 

Here is a demo: http://jsfiddle.net/7vrGu/2/

落在眉间の轻吻 2024-12-27 11:48:16

当您仅使用一个参数调用 hover 时,它会运行您在 mouseentermouseleave 事件中传入的处理函数。即,它是否将“.the_menu”滑动两次 - 一次当您鼠标悬停时,一次当您mouseleave时:
http://api.jquery.com/hover/#hover2

您需要调用 hover 带有两个参数,传递一个单独的处理函数来调用 mouseleave
http://api.jquery.com/hover/#hover1

示例:

$('img.menu_class').hover(function () {
        $('.the_menu').slideDown('medium');
    }, function() {
        // do something else in mouseleave - you don't necessarily want to slideUp ".the_menu" yet
    });

when you call hover with just one argument it runs the handler function you pass in on both mouseenter and mouseleave events. I.e. is it slideToggling ".the_menu" twice - once when you mouseover, once when you mouseleave:
http://api.jquery.com/hover/#hover2

You need to call hover with two arguments, pass a separate handler function to call on mouseleave.
http://api.jquery.com/hover/#hover1

example:

$('img.menu_class').hover(function () {
        $('.the_menu').slideDown('medium');
    }, function() {
        // do something else in mouseleave - you don't necessarily want to slideUp ".the_menu" yet
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文