当鼠标位于下拉菜单的子菜单中时,如何保持父菜单悬停

发布于 2024-10-05 05:02:26 字数 1686 浏览 2 评论 0原文

大家好,我想知道如何在子菜单中移动鼠标时保持父菜单悬停。

我是 jQuery 的初学者,我希望你能帮助我提供一些提示/建议。

菜单网站链接

jQuery 代码

// Navigation Slide //
var navHover = function () {
    $("#S" + this.id).animate({top: '-40px'}, 300, 'swing')
    $(this).animate({paddingTop: '30px'}, 300, 'swing').animate({paddingTop: '45px'}, 300, 'swing')
    $("#I" + this.id).animate({top: '-10px'}, 300, 'swing').animate({top: '0px'}, 300, 'swing')
}
var navRelease = function () {
    $("#S" + this.id).animate({top: '-130px'}, 300, 'swing');
}

$('#navInside a.topLevel').hover(navHover, navRelease);


// Dropdown animation       
            function mainmenu(){
            jQuery(" #navInside ul ").css({display: "none"}); // Opera Fix
            jQuery(" #navInside li").hover(function(){
                    jQuery(this).find('ul:first').css({visibility: "visible",display: "none"}).slideDown(500);
                    },function(){
                    jQuery(this).find('ul:first').css({visibility: "hidden"});
                    });
            }

             jQuery(document).ready(function(){                 
                mainmenu();
            });

导航 HTML

<div id="navInside">
<li><a class="topLevel" href="">Home</a></li>
<li><a class="topLevel" href="">Options</a>
    <ul>
      <li><a href="">Submenu 1</a></li>
      <li><a href="">Submenu 2</a></li>
    </ul>
</li>
<li><a class="topLevel" href="">Thanks</a></li>

Hi guys i wonder how to keep the parent menu hovered while moving mouse in the submenus.

I'm a beginner in jQuery and I like you to help me with some tip/suggestion.

LINK TO MENU WEBSITE

jQuery Code

// Navigation Slide //
var navHover = function () {
    $("#S" + this.id).animate({top: '-40px'}, 300, 'swing')
    $(this).animate({paddingTop: '30px'}, 300, 'swing').animate({paddingTop: '45px'}, 300, 'swing')
    $("#I" + this.id).animate({top: '-10px'}, 300, 'swing').animate({top: '0px'}, 300, 'swing')
}
var navRelease = function () {
    $("#S" + this.id).animate({top: '-130px'}, 300, 'swing');
}

$('#navInside a.topLevel').hover(navHover, navRelease);


// Dropdown animation       
            function mainmenu(){
            jQuery(" #navInside ul ").css({display: "none"}); // Opera Fix
            jQuery(" #navInside li").hover(function(){
                    jQuery(this).find('ul:first').css({visibility: "visible",display: "none"}).slideDown(500);
                    },function(){
                    jQuery(this).find('ul:first').css({visibility: "hidden"});
                    });
            }

             jQuery(document).ready(function(){                 
                mainmenu();
            });

Navigation HTML

<div id="navInside">
<li><a class="topLevel" href="">Home</a></li>
<li><a class="topLevel" href="">Options</a>
    <ul>
      <li><a href="">Submenu 1</a></li>
      <li><a href="">Submenu 2</a></li>
    </ul>
</li>
<li><a class="topLevel" href="">Thanks</a></li>

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

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

发布评论

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

评论(2

吐个泡泡 2024-10-12 05:02:26

问题是您的顶级悬停在 上元素。移动到子菜单会导致 上触发 mouseleave 事件。因为子菜单元素不是 的子元素,而是

  • 的子元素。试试这个:
  • $('#navInside a.topLevel').parent().hover(navHover, navRelease);
    

    顺便说一句 - 您可以通过在悬停函数中使用 $(this) 来简化 navHover/navRelease 代码。这样你就不需要元素上的特定 ID。您只需找到相对于当前元素的它们。

    The problem is that your top-level hover is on the <a> element. Moving to a submenu results in the mouseleave event firing on the <a> element since the submenu elements are not children of the <a>, but of the <li>. Try this instead:

    $('#navInside a.topLevel').parent().hover(navHover, navRelease);
    

    BTW - You can simplify your navHover/navRelease code by using the $(this) within the hover functions. That way you don't need specific ids on the elements. You would just find them relative to the current element.

    旧时光的容颜 2024-10-12 05:02:26

    你根本不需要 JS。以下是 jsFiddle 的解释:

    您想要显示一个 ul仅当您将鼠标悬停在 li 上时,它才是 li 的子级。这是由 li ul(鼠标悬停状态)和 li:hover ul(鼠标悬停状态)处理的。

    当您将鼠标悬停在其上时,LI 的高度会发生变化,因为您现在也显示 UL,因此只要您不离开其 (LI) 区域(包括其文本 + UL),它就会保持可见。


    如果您想要一些动画,请查看 CSS 过渡。浏览器支持参差不齐,但也许您想要做的事情会得到普遍支持。准确判断区域正在变化的元素上的鼠标悬停/移出事件可能很棘手。我建议仅当您确实需要这样做时才使用 JS。

    You don't need JS for this at all. Here's the jsFiddle for the explanation below:

    You want to show a ul that is the child of an li only when you're hovering over it. That is handled by li ul (mouseout state) and li:hover ul (mouseover state).

    When you hover over it, the height of the LI changes because you're now displaying the UL as well, so it will stay visible as long as you don't leave its (LIs) area (which includes its text + UL).


    If you want some animations, look at CSS transitions. Browser support is spotty, but maybe what you're trying to do will be supported universally. Accurately judging mouseover/out events on elements whose area is changing can be tricky. I'd recommend using JS only if you really need to for something like this.

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