MouseOver 删除类 MouseOut 恢复类

发布于 2024-11-04 08:42:14 字数 953 浏览 5 评论 0原文

因此,当我将鼠标悬停在其他菜单项上时,我试图找到一些脚本来删除活动菜单项上的类。但是当我离开导航时恢复它。棘手的部分是确保如果当前活动项目上有活动子菜单,则脚本不会触发。

例如,我们有 4 个菜单项(Main 1、Main 2、Main 3、Main 4)。其中两个有子菜单(Main 2、Main 4)。 Main 1 当前处于活动状态,并且有一个名为 active 的类。当您将鼠标悬停在 Main 2 上时,子菜单会显示,当悬停在其子菜单上时,Main 2 有一个悬停类和一个 hilite 类,但现在 Main 1 恢复到正常库存。现在您离开导航栏,Main 1 返回到活动类,Main 2 看起来像正常状态。

现在采用相同的设置并使 Main 2 处于活动状态,默认情况下显示其子菜单。当您将鼠标悬停在 Main 1 Main 3 Main 4 上时,子菜单将关闭,Main 2 上的活动类将被删除,但当您移开时,它会恢复为活动状态并显示其子菜单。现在,如果您将鼠标悬停在 Main 2 或其子菜单上,则该类永远不会更改,它会保持不变。

我已经广泛搜索了类似的设置,除了我在之前的问题上发布的内容之外找不到任何内容,可以找到这里 我扔掉了所有的东西,但不幸的是 stopPropagation() 这让我认为这是这个项目的错误代码,但它在一个截然不同的项目上工作。我对此提出了第二个问题,因为我不确定如果它已经被标记为已回答,我是否可以在另一个问题上发布。

预先感谢并希望我能得到一些帮助,
泰勒

编辑:这里是jsfiddle上旧脚本的链接这里 < strong>更新:2011 年 4 月 30 日

so i am trying to find a little bit of script to remove the class on the active menu item when i hover over other menu items. But restore it when i move away from the nav. The tricky part is making sure that the script does not trigger if there is a active sub menu on the current active item.

So for example we have 4 menu items (Main 1, Main 2, Main 3, Main 4). Two have submenus (Main 2, Main 4). Main 1 is currently active and has a class named active. When you hover over Main 2 the submenu shows and it Main 2 has a hover class and a hilite class when hovering its submenu but now Main 1 is back to normal stock. Now you move away from the nav and Main 1 goes back to having an active class and Main 2 looks like normal

Now take the same setup and make Main 2 active with it's submenu showing by default. When you hover over Main 1 Main 3 Main 4 the submenu closes and the active class on Main 2 gets removed but when you move away it returns to being active and showing its submenu. Now if you hover over Main 2 or it's submenu's the class never changes it stays the same.

I have searched far and wide for a similar setup and cannot find anything but what i had posted on an earlier question which can be found here i went threw all of my stuff and to no luck the stopPropagation() which makes me think that this is the wrong piece of code for this project but it worked on a far different project. I made a second question on this because i was not sure if i could post on the other if it was already marked as answered.

Thanks in advance and hope i get some help,
Tyler

EDIT: here is a link to the old script on jsfiddle here UPDATED: 4/30/2011

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

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

发布评论

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

评论(1

一花一树开 2024-11-11 08:42:14

请注意,这仅适用于 WordPress
请在 jsfiddle 上发布编辑,而不是在此代码上发布编辑,除非它是主要的

好的,所以我们终于有了一个工作菜单。我做了一些认真的搜索并找到了解决办法。现在我不确定它是否正确,但它的工作原理就像我想要的那样。如果有人想使用它,请随意。它与 WordPress 一起使用,因此我有一个添加几行代码的功能,并将在下面发布。

这是我添加到 WordPress 的函数:

function additional_active_item_classes($classes = array(), $menu_item = false){

    if(in_array('current-menu-item', $menu_item->classes)){
        $classes[] = 'active active-menu';
    }
    return $classes;
}  
add_filter( 'nav_menu_css_class', 'additional_active_item_classes', 10, 2 );

上面的代码适用于 wordpress 3.0+

这将向当前菜单项添加一个类

到目前为止,我已经在 jQUERY 中添加了一组东西,并且有可能是一种将它们合并在一起并使其作为一个整体工作的方法,但我不知道如何做到这一点,但我会在此处发布代码,然后将演示发布到 jsfiddle

这是我拼凑在一起的 jQUERY 脚本,并将添加效果在demo中发现:

这会向所有父级和父级添加一个 .not-active 类。非父项:

$('*:not(li) > ul#navbar > li').addClass('not-active');

这会向所有 ul.sub-menus 添加一个 .child-menu 类:

$('ul.sub-menu').children().addClass('child-menu');

这会向每个子菜单中的第一个子项添加一个 .first-child 类子菜单(为额外样式添加):

$('#navbar ul.sub-menu :first-child').addClass('first-child');

这将 .last-child 类添加到每个子菜单中的最后一个子项(为额外样式添加):

$('#navbar ul.sub-menu :last-child').addClass('last-child');

这将在活动菜单上切换类 .not-active (删除下面最后一个脚本的 .not-active):

$("#navbar li.active").toggleClass("not-active");

这将在活动子菜单上切换 .not-active 类(删除下面最后一个脚本的 .not-active): >

$('#navbar ul.sub-menu li.active').toggleClass("not-active");

如果 .current-menu-parent 存在,则添加 .parent-active 类:

$('ul#navbar li.current-menu-parent').addClass('parent-active');

如果 li.current-menu-parent ul.sub-menu,则添加 .child-active 类存在:

$('ul#navbar li.current-menu-parent ul.sub-menu').addClass('child-active');

*
这会在悬停其子菜单时向父级添加 .hilite 类:*

$('#navbar li').hover(
    function() {
        $(this).parents('li').children('a').addClass('hilite');
    },
    function(){
        $(this).parents('li').children('a').removeClass('hilite');
    }
);

这会在悬停其他菜单项时删除活动菜单的 .active 类,但在悬停活动项目或活动子项目时不会使用item:

$('#navbar li.not-active').hover(
    function() {
        $('#navbar .active').removeClass('active-menu');
        $('#navbar li.current-menu-parent, #navbar li.current-menu-parent ul.sub-menu').removeClass('parent-active child-active');
    },
    function() {
        $('#navbar .active').addClass('active-menu');
        $('#navbar li.current-menu-parent, #navbar li.current-menu-parent ul.sub-menu').addClass('parent-active child-active');
   }
);

现在我们有了 jQUERY,让我们获取 css 和 html 以供参考。

这是 css:

/*Main position of menu*/
#navigation{float: left; margin: 5px 0 15px; padding: 0; position: relative;  width: 100%}
/*First UL tag for menu*/
ul#navbar{margin: 0; padding: 0; font-size: 96%; height: 20px; background: #009AD9}
/*Sets style for each LI tag and sets effects*/
#navbar li{float: left; list-style: none; text-transform: uppercase; font-weight: bold}
#navbar li a{display: block; text-decoration: none; color: #fff; padding: .24em .8em; line-height: normal}
#navbar li:hover a:hover, #navbar li a.hilite{background: #fff; color: #000}
#navbar li.active-menu a{background: #fff; border-top: 2px solid #B70F0F; padding-top: .063em; color: #666}
/*Sets parent active when child is active*/
#navbar li.parent-active a{background: #fff; color: #666}
/*Sets child-menu to not display when not-active*/
ul.sub-menu{display: none}
/*Sets the position of child menu using first-child (leave to avoid conflict with other style's)*/
#navbar li ul.sub-menu li.first-child{margin-left: -0.375em}
/*Sets child as visible on parent hover*/
#navbar li:hover ul.sub-menu{display: inline; left: 0; margin: 0; padding: 0;width: 980px; position: absolute}
/*Sets child as visible when parent is active menu*/
#navbar li.active-menu ul.sub-menu{display: inline; left: 0; margin: 0; padding: 0;width: 980px; position: absolute}
/*Sets child menu active when active item is inside of child menu*/
ul.child-active{display: inline; left: 0; margin: 0 0 0 .375em; padding: 0; position: absolute}
/*Sets style of child links*/
#navbar li ul.sub-menu li a{display: block; text-decoration: none; padding: 0 .5em; line-height: normal}
/*Sets various child link styles*/
ul.sub-menu li a{color: #666!important}
ul.sub-menu li a:hover{color: #000!important}
li.active-menu ul.sub-menu a{border-top: none!important; padding: 0 .5em!important}
#navbar li.current-menu-parent:hover ul.sub-menu{display: inline; left: 0; margin: 0 0 0 .375em; padding: 0; position: absolute}

它应该非常简单阅读

这是 html:

<div id="navigation">
    <ul id="navbar">
        <li class="not-active"><a href="#">Main 1</a></li>
        <li class="active active-menu"><a href="#">Main 2</a>
            <ul class="sub-menu">
                <li class="child-menu first-child"><a href="#">Sub 1</a></li>
                <li class="child-menu"><a href="#">Sub 2</a></li>
                <li class="child-menu last-child"><a href="#">Sub 3</a></li>
            </ul>
        </li>
        <li class="not-active"><a href="#">Main 3</a></li>
        <li class="not-active"><a href="#">Main 4</a>
            <ul class="sub-menu">
                <li class="child-menu first-child"><a href="#">Sub 4</a></li>
                <li class="child-menu"><a href="#">Sub 5</a></li>
                <li class="child-menu last-child"><a href="#">Sub 6</a></li>
            </ul>
        </li>
    </ul>
</div>

请随意使用此代码,如果您找到一种更简单的方法来对所有这些脚本进行分组,请这样做。

您可以在JSFIDDLE处观看现场演示 >

Note that this will only work on wordpress
Please post edits on jsfiddle and not on this code unless it is major

Ok so we have a working menu finally. I have done some serious searching and found a fix. Now i am not sure if it is correct or not but it works like i want it to. If any one wants to use it feel free. It is being used with WordPress so i have a function to add a few lines of code and will post that below as well.

Here is the function that i added to WordPress:

function additional_active_item_classes($classes = array(), $menu_item = false){

    if(in_array('current-menu-item', $menu_item->classes)){
        $classes[] = 'active active-menu';
    }
    return $classes;
}  
add_filter( 'nav_menu_css_class', 'additional_active_item_classes', 10, 2 );

The above code is for wordpress 3.0+

This will add a class to the current menu item

So far the jQUERY i have kind of added a cluster of things and there is probably a way to merge it all together and make it work as one but i do not know how to do this but ill post the code here and then a demo to jsfiddle

Here is the jQUERY script that i pieced together and will add the effect found in the demo:

This adds a .not-active class to all parent & non-parent items:

$('*:not(li) > ul#navbar > li').addClass('not-active');

This adds a .child-menu class to all ul.sub-menus:

$('ul.sub-menu').children().addClass('child-menu');

This adds a .first-child class to the first child item in each sub-menu (added for extra styling):

$('#navbar ul.sub-menu :first-child').addClass('first-child');

This adds a .last-child class to the last child item in each sub-menu (added for extra styling):

$('#navbar ul.sub-menu :last-child').addClass('last-child');

This will toggle the class .not-active on an active menu (removes .not-active for the last script below):

$("#navbar li.active").toggleClass("not-active");

This will toggle the class .not-active on an active child menu (removes .not-active for the last script below):

$('#navbar ul.sub-menu li.active').toggleClass("not-active");

This adds a .parent-active class if .current-menu-parent is present:

$('ul#navbar li.current-menu-parent').addClass('parent-active');

This adds a .child-active class if li.current-menu-parent ul.sub-menu is present:

$('ul#navbar li.current-menu-parent ul.sub-menu').addClass('child-active');

*
This adds a .hilite class to the parent when hovering it's child menu:*

$('#navbar li').hover(
    function() {
        $(this).parents('li').children('a').addClass('hilite');
    },
    function(){
        $(this).parents('li').children('a').removeClass('hilite');
    }
);

This removes the .active class of an active menu when hovering other menu items but will not be used when hovering the active item or child items of the active item:

$('#navbar li.not-active').hover(
    function() {
        $('#navbar .active').removeClass('active-menu');
        $('#navbar li.current-menu-parent, #navbar li.current-menu-parent ul.sub-menu').removeClass('parent-active child-active');
    },
    function() {
        $('#navbar .active').addClass('active-menu');
        $('#navbar li.current-menu-parent, #navbar li.current-menu-parent ul.sub-menu').addClass('parent-active child-active');
   }
);

So now that we have the jQUERY lets get the css and html in for reference.

Here is the css:

/*Main position of menu*/
#navigation{float: left; margin: 5px 0 15px; padding: 0; position: relative;  width: 100%}
/*First UL tag for menu*/
ul#navbar{margin: 0; padding: 0; font-size: 96%; height: 20px; background: #009AD9}
/*Sets style for each LI tag and sets effects*/
#navbar li{float: left; list-style: none; text-transform: uppercase; font-weight: bold}
#navbar li a{display: block; text-decoration: none; color: #fff; padding: .24em .8em; line-height: normal}
#navbar li:hover a:hover, #navbar li a.hilite{background: #fff; color: #000}
#navbar li.active-menu a{background: #fff; border-top: 2px solid #B70F0F; padding-top: .063em; color: #666}
/*Sets parent active when child is active*/
#navbar li.parent-active a{background: #fff; color: #666}
/*Sets child-menu to not display when not-active*/
ul.sub-menu{display: none}
/*Sets the position of child menu using first-child (leave to avoid conflict with other style's)*/
#navbar li ul.sub-menu li.first-child{margin-left: -0.375em}
/*Sets child as visible on parent hover*/
#navbar li:hover ul.sub-menu{display: inline; left: 0; margin: 0; padding: 0;width: 980px; position: absolute}
/*Sets child as visible when parent is active menu*/
#navbar li.active-menu ul.sub-menu{display: inline; left: 0; margin: 0; padding: 0;width: 980px; position: absolute}
/*Sets child menu active when active item is inside of child menu*/
ul.child-active{display: inline; left: 0; margin: 0 0 0 .375em; padding: 0; position: absolute}
/*Sets style of child links*/
#navbar li ul.sub-menu li a{display: block; text-decoration: none; padding: 0 .5em; line-height: normal}
/*Sets various child link styles*/
ul.sub-menu li a{color: #666!important}
ul.sub-menu li a:hover{color: #000!important}
li.active-menu ul.sub-menu a{border-top: none!important; padding: 0 .5em!important}
#navbar li.current-menu-parent:hover ul.sub-menu{display: inline; left: 0; margin: 0 0 0 .375em; padding: 0; position: absolute}

It should be pretty simple to read

Here is the html:

<div id="navigation">
    <ul id="navbar">
        <li class="not-active"><a href="#">Main 1</a></li>
        <li class="active active-menu"><a href="#">Main 2</a>
            <ul class="sub-menu">
                <li class="child-menu first-child"><a href="#">Sub 1</a></li>
                <li class="child-menu"><a href="#">Sub 2</a></li>
                <li class="child-menu last-child"><a href="#">Sub 3</a></li>
            </ul>
        </li>
        <li class="not-active"><a href="#">Main 3</a></li>
        <li class="not-active"><a href="#">Main 4</a>
            <ul class="sub-menu">
                <li class="child-menu first-child"><a href="#">Sub 4</a></li>
                <li class="child-menu"><a href="#">Sub 5</a></li>
                <li class="child-menu last-child"><a href="#">Sub 6</a></li>
            </ul>
        </li>
    </ul>
</div>

Please feel free to use this code and if you find a simpler way to group all of those scripts then please do.

You can see a live demo here at JSFIDDLE

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