带有鼠标悬停/鼠标悬停的 jQuery 手风琴导航

发布于 2024-08-27 04:16:24 字数 4438 浏览 6 评论 0原文

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

将鼠标悬停在父 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 -->

如果有任何帮助,我将非常感激!

I'm trying to create an accordion navigation menu with the following properties:

Hover over a parent LI, and its sub-menu slides down. If you mouse down to hover over the sub-menu, it stays open as you'd expect.

If you move your cursor off of either the parent link or the sub-menu, the sub-menu slides up again.

I thought I was getting close - I can get the sub-menu to slideDown, but when I hover over it, I've had to mouseout on the parent LI, triggering a slideUp. I just can't figure out how to make this do what I need it to!

Here's my script:

<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>

And here's my HTML markup:

<!-- 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 -->

I'd really appreciate any help, please!

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

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

发布评论

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

评论(4

圈圈圆圆圈圈 2024-09-03 04:16:25

我建议使用 jQuery UI 的出色手风琴(具有悬停功能):http://jqueryui。 com/demos/accordion/#hoverintent

I would suggest to use the splendid accordion (with hover functionality) of jQuery UI: http://jqueryui.com/demos/accordion/#hoverintent

千柳 2024-09-03 04:16:25

不确定您的页面上有什么,但这似乎适用于您提供的代码。已经晚了并且没有经过充分测试,但请尝试一下:

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

还要确保删除您的鼠标悬停代码

Not sure what all is on your page but this seemed to work with what code you supplied. It's late and not fully tested but give this a shot:

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

Also make sure to get rid of your mouseout code

去了角落 2024-09-03 04:16:25

我知道这是一篇旧帖子,但我想我会为其他寻求帮助的人做出贡献。对于新版本的 jQuery,您确实应该使用 .on() 事件处理程序而不是 .hover()。这是我在网站上使用的一个示例......

$('.leftnav-item:not(.active)').on({

    mouseenter: function() {
        $(this).find('.nav-sublock').stop(true,true).slideDown(500);
    },

    mouseleave: function() {
        $(this).find('.nav-sublock').slideUp(500);
    }

});

I know this is an old post, but I thought I would contribute for anyone else looking for help. You really should be using the .on() event handler instead of .hover() for new versions of jQuery. Here is an example from one I am using on a site...

$('.leftnav-item:not(.active)').on({

    mouseenter: function() {
        $(this).find('.nav-sublock').stop(true,true).slideDown(500);
    },

    mouseleave: function() {
        $(this).find('.nav-sublock').slideUp(500);
    }

});
巨坚强 2024-09-03 04:16:24

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

$("#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
}
);

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

OK, thanks for anyone who looked. I've pretty much managed to sort this out now. I've replaced the jQuery block containing mouseover and mouseout with:

$("#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
}
);

It's a little jerky if you run the cursor back and forth over it, but it at least works. I'd like to properly stop the animation queue if possible though.

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