MouseOver 删除类 MouseOut 恢复类
因此,当我将鼠标悬停在其他菜单项上时,我试图找到一些脚本来删除活动菜单项上的类。但是当我离开导航时恢复它。棘手的部分是确保如果当前活动项目上有活动子菜单,则脚本不会触发。
例如,我们有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,这仅适用于 WordPress
请在 jsfiddle 上发布编辑,而不是在此代码上发布编辑,除非它是主要的
好的,所以我们终于有了一个工作菜单。我做了一些认真的搜索并找到了解决办法。现在我不确定它是否正确,但它的工作原理就像我想要的那样。如果有人想使用它,请随意。它与 WordPress 一起使用,因此我有一个添加几行代码的功能,并将在下面发布。
这是我添加到 WordPress 的函数:
上面的代码适用于 wordpress 3.0+
这将向当前菜单项添加一个类
到目前为止,我已经在 jQUERY 中添加了一组东西,并且有可能是一种将它们合并在一起并使其作为一个整体工作的方法,但我不知道如何做到这一点,但我会在此处发布代码,然后将演示发布到 jsfiddle
这是我拼凑在一起的 jQUERY 脚本,并将添加效果在demo中发现:
这会向所有父级和父级添加一个 .not-active 类。非父项:
这会向所有 ul.sub-menus 添加一个 .child-menu 类:
这会向每个子菜单中的第一个子项添加一个 .first-child 类子菜单(为额外样式添加):
这将 .last-child 类添加到每个子菜单中的最后一个子项(为额外样式添加):
这将在活动菜单上切换类 .not-active (删除下面最后一个脚本的 .not-active):
这将在活动子菜单上切换 .not-active 类(删除下面最后一个脚本的 .not-active): >
如果 .current-menu-parent 存在,则添加 .parent-active 类:
如果 li.current-menu-parent ul.sub-menu,则添加 .child-active 类存在:
*
这会在悬停其子菜单时向父级添加 .hilite 类:*
这会在悬停其他菜单项时删除活动菜单的 .active 类,但在悬停活动项目或活动子项目时不会使用item:
现在我们有了 jQUERY,让我们获取 css 和 html 以供参考。
这是 css:
它应该非常简单阅读
这是 html:
请随意使用此代码,如果您找到一种更简单的方法来对所有这些脚本进行分组,请这样做。
您可以在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:
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:
This adds a .child-menu class to all ul.sub-menus:
This adds a .first-child class to the first child item in each sub-menu (added for extra styling):
This adds a .last-child class to the last child item in each sub-menu (added for extra styling):
This will toggle the class .not-active on an active menu (removes .not-active for the last script below):
This will toggle the class .not-active on an active child menu (removes .not-active for the last script below):
This adds a .parent-active class if .current-menu-parent is present:
This adds a .child-active class if li.current-menu-parent ul.sub-menu is present:
*
This adds a .hilite class to the parent when hovering it's child menu:*
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:
So now that we have the jQUERY lets get the css and html in for reference.
Here is the css:
It should be pretty simple to read
Here is the html:
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