具有子 LI 元素的 JQuery 悬停效果

发布于 2024-10-19 04:09:22 字数 574 浏览 4 评论 0原文

我对 JavaScript 很陌生,更不用说 JQuery了,尽管我学得很慢,但我遇到了悬停效果的问题。我正在使用一个名为 lavalamp 的插件,它在多维菜单方面给我带来了一段时间的问题。我尝试过 .noLava 但没有成功,所以我决定尝试在子 UL 层悬停时隐藏跟踪 LI 元素(因为当配音层悬停时,跟踪 LI 会跟随在不自然的位置)。

$(" #top_menu ul li ul").hover(function(){
        $('li.backLava').hide(300) 
    },
    function(){
        $('li.backLava').show(100) 
    }
);

一旦我将鼠标悬停在子菜单上,此代码就可以工作,但问题是,当我转到另一个子菜单然后返回第一个子菜单时,跟踪 LI 将再次显示。另外,当我将子菜单悬停到页面上时,它有时不会显示回来。尽管在获得 JS 和 JQuery 技能的同时尝试做这个菜单是一次很好的经历。它现在开始变得不再是一个笑话,我拥有 PHP、CSS、HTML、C# 等方面的技能。然而 JS 似乎并不像有时要求的那样做。

所以任何帮助将不胜感激,谢谢。

I'm very new to JavaScript let alone JQuery, Although I've been picking it up slowly I have come across a problem with a hover effect. I'm am using a plugin called lavalamp and its been giving me problems for a while regarding a multi-dimensional menu. I have tried .noLava without success so I decided to try and hide the tracking LI element once the sub UL layer was hovered (because the moment a dub layer was hovered the tracking LI would follow in unnatural places).

$(" #top_menu ul li ul").hover(function(){
        $('li.backLava').hide(300) 
    },
    function(){
        $('li.backLava').show(100) 
    }
);

This code works once I hover the sub menus, yet the problems is that when i goto another sub menu then back to the first sub menu, the tracking LI will show again. also when i hover out the sub menu to the page, it sometimes wont show back. Although trying to do this menu has been a good experience while gaining skills in JS and JQuery. Its now starting to become beyond a joke, I have skills in PHP, CSS, HTML, C# etc. Yet JS just doesn't seem like it does whats being asked sometimes..

So any help will greatly be appreciated thanks.

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

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

发布评论

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

评论(1

风月客 2024-10-26 04:09:22

您看到的问题在于您在悬停函数中使用的 JQuery 选择器。当您在“unhover”函数上使用“li.backLava”时,您是在告诉任何具有“backLava”类的列表项元素再次显示,这就是为什么当子菜单隐藏时,跟踪 LI 会再次出现。

为了让它按照您想要的方式工作,我们只需要改进您的代码以仅隐藏父跟踪 LI 元素。因此,不要只使用“li.backLava”,而是使用更具体的东西:

$("#top_menu ul li ul").hover(
    function(){

        //Find this sub-menu's parent list, and hide the tracking LI.
        $($(this).parents("ul")[0]).children("li.backLava").hide();
    },
    function(){

        //Find this sub-menu's parent list, and show the tracking LI again.
        $($(this).parents("ul")[0]).children("li.backLava").show();
    }
);

注意:此代码未经测试,但它应该可以工作,或者可以与一些小的调整。

编辑
既然我已经看到了您的演示和您想要的内容,我已经更新了代码。更新后的代码应该可以满足您的需求。

The problem you're seeing resides in the JQuery selector you're using in your hover functions. When you use "li.backLava" on the "unhover" function, you're telling any list item elements with the class "backLava" to show again, which is why the tracking LI appears again when a sub-menu hides.

To get it to work as you want, we just need to refine your code to hide only the parent tracking LI element. So instead of just using "li.backLava", use something that's a bit more specific:

$("#top_menu ul li ul").hover(
    function(){

        //Find this sub-menu's parent list, and hide the tracking LI.
        $($(this).parents("ul")[0]).children("li.backLava").hide();
    },
    function(){

        //Find this sub-menu's parent list, and show the tracking LI again.
        $($(this).parents("ul")[0]).children("li.backLava").show();
    }
);

NOTE: This code isn't tested, but it should work, or work with some minor adjustment.

EDIT
I've updated the code now that I've seen your demo and what you want. The updated code should do what you want.

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