带有鼠标悬停/鼠标悬停的 jQuery Accordion 导航

标签 jquery navigation accordion mouseover mouseout

我正在尝试创建具有以下属性的 Accordion 导航菜单:

将鼠标悬停在父 LI 上,其子菜单会向下滑动。如果您将鼠标向下悬停在子菜单上,它会按照您的预期保持打开状态。

如果您将光标移离父链接或子菜单,子菜单会再次向上滑动。

我以为我已经很接近了 - 我可以让子菜单向下滑动,但是当我将鼠标悬停在它上面时,我必须将鼠标悬停在父 LI 上,从而触发向上滑动。我只是不知道如何让它完成我需要的事情!

这是我的脚本:

<script type="text/javascript">

$(document).ready(function() {

//If its a page parent (based off wordpress), add the class "display-list"
//This way the accordion will be opened up on the page you are on.
if ($('#nav li').hasClass("current_page_parent")) {
$('#nav .current_page_parent ul').addClass("display-list"); }

// Hide the submenus
$('#nav li ul').hide();
// Add a class to the parent li IF it has sub UL's
$("#nav li:has(ul)").addClass("nav-parent");
// Add a class to sub-UL if it has a parent LI
$("#nav li ul").addClass("nav-child");

// When you hover over the link in the parent LI...
$('#nav li.nav-parent').mouseover( function() {
// ...slide the sub-UL into view, and remove the 'display-list' class
$(this).children('.nav-child').slideDown(1000).removeClass("display-list");
}).mouseout( function() {
$(this).children('.nav-child').slideUp(1000);
});

});
</script>

这是我的 HTML 标记:

<!-- Start: Navigation -->
<ul id="nav">
<li class="hidden"><a href="#main">Skip to Content</a></li>
<li class="page_item page-item-2 current_page_item"><a href="http://www.examplewebsite.com">Home</a></li>
<li class="page_item page-item-44"><a href="http://www.examplewebsite.com/who-we-are/">Who we are</a>
    <ul>
        <li class="page_item page-item-99"><a href="http://www.examplewebsite.com/who-we-are/partners-consultants-and-alliance-partners/">Partners, Consultants and Alliance Partners</a></li>
        <li class="page_item page-item-105"><a href="http://www.examplewebsite.com/who-we-are/who-we-work-with/">Who we work with</a></li>
        <li class="page_item page-item-107"><a href="http://www.examplewebsite.com/who-we-are/coffey-graham-north-star-alliance/">Coffey Graham North Star Alliance</a></li>
    </ul>
</li>
<li class="page_item page-item-47"><a href="http://www.examplewebsite.com/what-we-do/">What we do</a>
    <ul>
        <li class="page_item page-item-48"><a href="http://www.examplewebsite.com/what-we-do/technology-transactions-and-outsourcing/">Technology transactions and Outsourcing</a></li>
        <li class="page_item page-item-53"><a href="http://www.examplewebsite.com/what-we-do/dispute-resolution-and-avoidance-arbitration-mediation-and-litigation/">Dispute Resolution and Avoidance: Arbitration, Mediation and Litigation</a></li>
        <li class="page_item page-item-56"><a href="http://www.examplewebsite.com/what-we-do/company-commercial-and-private-equity-work/">Company/Commercial and Private Equity Work</a></li>
        <li class="page_item page-item-314"><a href="http://www.examplewebsite.com/what-we-do/virtual-general-counsel-service/">Virtual General Counsel Service</a></li>
        <li class="page_item page-item-117"><a href="http://www.examplewebsite.com/what-we-do/training-and-coaching/">Training and coaching</a></li>
    </ul>
</li>
<li class="page_item page-item-59"><a href="http://www.examplewebsite.com/see-us-in-action/">See us in action</a>
    <ul>
        <li class="page_item page-item-152"><a href="http://www.examplewebsite.com/see-us-in-action/blog/">Blog</a></li>
        <li class="page_item page-item-154"><a href="http://www.examplewebsite.com/see-us-in-action/podcasts/">Podcasts</a></li>
        <li class="page_item page-item-150"><a href="http://www.examplewebsite.com/see-us-in-action/training-and-coaching/">Training and coaching</a></li>
        <li class="page_item page-item-160"><a href="http://www.examplewebsite.com/see-us-in-action/publications/">Publications</a></li>
    </ul>
</li>
<li class="page_item page-item-69"><a href="http://www.examplewebsite.com/contact/">Contact</a></li>
</ul>
<!-- End: Navigation -->

真的非常感谢您的帮助!

最佳答案

好的,感谢所有看过的人。我现在已经基本解决了这个问题。我已将包含 mouseovermouseout 的 jQuery block 替换为:

$("#nav li.nav-parent").hover(
function () { 
$(this).children('.nav-child').stop(true,true).slideDown(1000).removeClass("display-list"); // fired on mouseover
},
function () {
$(this).children('.nav-child').slideUp(1000); // fired on mouseout
}
);

如果你在上面来回移动光标,会有点不稳定,但它至少可以工作。如果可能的话,我想正确停止动画队列。

关于带有鼠标悬停/鼠标悬停的 jQuery Accordion 导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2539604/

相关文章:

swift - Swiftui 中的汉堡导航菜单幻灯片动画

java - 抽屉导航 xml 中重叠的 Activity 布局

html - 在 Accordion 标题中插入指向内部页面的链接

css - 使用 CSS 的狮身人面像和 Accordion 列表

javascript - DIV 重新加载后 JQuery Fancybox 不工作

javascript - 使用自定义按钮过滤结果

navigation - Prism 导航: where to register View/ViewModels with UnityContainer?

jQuery 滑动切换,关闭其他 Accordion 类型?

php - 实时显示另一个网站上发布的数据

javascript - 使用 javascript 日期时在 safari 上获取 NAN