jQuery菜单效果出错了

发布于 2024-11-07 01:37:37 字数 446 浏览 1 评论 0原文

我正在寻找一种方法来淡出列表排序导航中的子菜单,因为我希望它淡出:

总是有一个活动菜单项 - 如果活动菜单项位于子菜单中 - 也是一个活动菜单父项。因此,在初始化时,具有活动当前项目或当前父项目的子菜单应该是可见的。如果我将鼠标悬停在其他菜单项上,其子菜单将淡入,活动菜单将淡出。离开整个菜单后,活动子菜单再次淡入。另外,我使用 jQuery 插件hoverIntent (http://cherne.net/brian/resources/jquery.hoverIntent.html) 为每个悬停操作指定超时。 这是我到目前为止所得到的,但它确实有问题。由于某些原因,子菜单有时会完全消失,而且当悬停两个项目而不离开导航时,两个子菜单会重叠。有人对我有想法或建议吗? =) 请在此处查看演示:http://jsfiddle.net/BQuPz/

I am seeking for a way to fade a submenu in a list-ordered navigation as I want it to fade:

There is always an active menu item - and if the active menu item is in the submenu - also an active menu parent. So, at init the submenu with an active current item or current parent item should be visible. If I hover an other menu item, its submenu will fade in, the active one fades out. After leaving the whole menu, the active submenu fades in again. Plus, I used the jQuery plugin hoverIntent (http://cherne.net/brian/resources/jquery.hoverIntent.html) to give a timeout for each hover action.
This is what I got so far, but it's really buggy. For some reasons, submenus are sometimes just disappearing completely, also when hovering two items without leaving the navigation, two submenus overlap. Does anyone have an idea or tip for me? =) Please see a demo HERE: http://jsfiddle.net/BQuPz/

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

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

发布评论

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

评论(2

最冷一天 2024-11-14 01:37:37

CSS 问题:最好不要自己使用 display:noneopacity,将其留给 jQuery 并为 使用正确的函数。代码>fadeIn()fadeOut()

而不是放置 display:block; float:left; 在菜单的

  • 元素上,您应该使用 display:inline-block。如果浮动
  • ,则会有效地将它们从父容器中分离出来,使 ul 的大小为零,因此无法侦听 mouseleave 事件除非你明确设置它的大小。
  • 代码问题:在hoverIntent插件中,超时可能不是您想要的。 Timeout 在一定时间后会自动触发 out 事件。要延迟悬停效果,请改用sensitivityinterval。查看文档页面

    如果应用上述修复,唯一需要的事件是hoverIntent 的over 和主导航的mouseleave 。 over 事件淡入当前子菜单并淡出其余子菜单,当用户将鼠标悬停在导航上时,mouseleave 淡入活动子菜单。查看修改后的 jsfiddle 演示: http://jsfiddle.net/BQuPz/4/

    // Hide inactive ones
    $('#main-nav > li:not(.current-menu-parent) .sub-menu').hide();
    
    // What to do when hovering over a menu item
    $("#main-nav > li").hoverIntent({
        over: function(){
            // Find the hovered menu's sub-menu
            var $submenu = $(this).find('.sub-menu');
    
            // Stop the animation and fade out the other submenus
            $('.sub-menu').not($submenu).stop(true,true).fadeOut(260);
    
            // Fade in the current one
            $submenu.fadeIn(260);
        }
    });
    
    // What to do when user leaves the navigation area
    $('#main-nav').mouseleave(function(){
        // Fade out all inactive submenus
        $('#main-nav > li:not(.current-menu-parent) .sub-menu').fadeOut(260);
    
        // Fade in the active one
        $('.current-menu-parent .sub-menu').fadeIn(260);
    });
    

    Problems with CSS: It's best not to play with display:none and opacity yourself, leave it up to jQuery and use the proper functions for fadeIn() and fadeOut().

    Instead of putting display:block; float:left; on the menu's <li> elements, you should use display:inline-block. If you float the <li>s, that effectively tears them out of their parent container, leaving the ul with zero size, hence it is impossible to listen to mouseleave events unless you explicitly set its size.

    Problems with code: In the hoverIntent plugin, timeout might not be what you're looking for. Timeout automatically fires the out event after a certain amount of time. To delay the hover effect, use sensitivity and interval instead. Check out the documentation page.

    If you apply the above fixes, the only events needed are hoverIntent's over and the main navigation's mouseleave. The over event fades in the current submenu and fades out the rest, the mouseleave fades in the active submenu when the user hovers off the navigation. Look at your modified jsfiddle demo: http://jsfiddle.net/BQuPz/4/

    // Hide inactive ones
    $('#main-nav > li:not(.current-menu-parent) .sub-menu').hide();
    
    // What to do when hovering over a menu item
    $("#main-nav > li").hoverIntent({
        over: function(){
            // Find the hovered menu's sub-menu
            var $submenu = $(this).find('.sub-menu');
    
            // Stop the animation and fade out the other submenus
            $('.sub-menu').not($submenu).stop(true,true).fadeOut(260);
    
            // Fade in the current one
            $submenu.fadeIn(260);
        }
    });
    
    // What to do when user leaves the navigation area
    $('#main-nav').mouseleave(function(){
        // Fade out all inactive submenus
        $('#main-nav > li:not(.current-menu-parent) .sub-menu').fadeOut(260);
    
        // Fade in the active one
        $('.current-menu-parent .sub-menu').fadeIn(260);
    });
    
    人生戏 2024-11-14 01:37:37

    而不是在 $(this).find("ul").animate({'opacity': '1'}, 250); 中使用 animate
    我会使用带有回调的 fadeIn 和 fadeOut 函数。然后您确定淡入动画在淡出结束后开始

    element.fadeOut(250,function(){otherElement.fadeIn(250)})
    

    Instead of using animate in $(this).find("ul").animate({'opacity': '1'}, 250);
    I would have used fadeIn and fadeOut functions with callbacks. Then you're sure your fadeIn animation begins after the fadeout ends

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