jQuery菜单效果出错了
我正在寻找一种方法来淡出列表排序导航中的子菜单,因为我希望它淡出:
总是有一个活动菜单项 - 如果活动菜单项位于子菜单中 - 也是一个活动菜单父项。因此,在初始化时,具有活动当前项目或当前父项目的子菜单应该是可见的。如果我将鼠标悬停在其他菜单项上,其子菜单将淡入,活动菜单将淡出。离开整个菜单后,活动子菜单再次淡入。另外,我使用 jQuery 插件hoverIntent (http://cherne.net/brian/resources/jquery.hoverIntent.html) 为每个悬停操作指定超时。 这是我到目前为止所得到的,但它确实有问题。由于某些原因,子菜单有时会完全消失,而且当悬停两个项目而不离开导航时,两个子菜单会重叠。有人对我有想法或建议吗? =) 请在此处查看演示:http://jsfiddle.net/BQuPz/
I am seeking for a way to fade a submenu in a list-ordered navigation as I want it to fade:
There is always an active menu item - and if the active menu item is in the submenu - also an active menu parent. So, at init the submenu with an active current item or current parent item should be visible. If I hover an other menu item, its submenu will fade in, the active one fades out. After leaving the whole menu, the active submenu fades in again. Plus, I used the jQuery plugin hoverIntent (http://cherne.net/brian/resources/jquery.hoverIntent.html) to give a timeout for each hover action.
This is what I got so far, but it's really buggy. For some reasons, submenus are sometimes just disappearing completely, also when hovering two items without leaving the navigation, two submenus overlap. Does anyone have an idea or tip for me? =) Please see a demo HERE: http://jsfiddle.net/BQuPz/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CSS 问题:最好不要自己使用
display:none
和opacity
,将其留给 jQuery 并为使用正确的函数。代码>fadeIn()
和fadeOut()
。而不是放置
display:block; float:left;
在菜单的元素上,您应该使用
display:inline-block
。如果浮动mouseleave
事件除非你明确设置它的大小。代码问题:在hoverIntent插件中,
超时
可能不是您想要的。 Timeout 在一定时间后会自动触发out
事件。要延迟悬停效果,请改用sensitivity
和interval
。查看文档页面。如果应用上述修复,唯一需要的事件是hoverIntent 的over 和主导航的mouseleave 。
over
事件淡入当前子菜单并淡出其余子菜单,当用户将鼠标悬停在导航上时,mouseleave
淡入活动子菜单。查看修改后的 jsfiddle 演示: http://jsfiddle.net/BQuPz/4/Problems with CSS: It's best not to play with
display:none
andopacity
yourself, leave it up to jQuery and use the proper functions forfadeIn()
andfadeOut()
.Instead of putting
display:block; float:left;
on the menu's<li>
elements, you should usedisplay:inline-block
. If you float the<li>
s, that effectively tears them out of their parent container, leaving the ul with zero size, hence it is impossible to listen tomouseleave
events unless you explicitly set its size.Problems with code: In the hoverIntent plugin,
timeout
might not be what you're looking for. Timeout automatically fires theout
event after a certain amount of time. To delay the hover effect, usesensitivity
andinterval
instead. Check out the documentation page.If you apply the above fixes, the only events needed are hoverIntent's over and the main navigation's mouseleave. The
over
event fades in the current submenu and fades out the rest, themouseleave
fades in the active submenu when the user hovers off the navigation. Look at your modified jsfiddle demo: http://jsfiddle.net/BQuPz/4/而不是在
$(this).find("ul").animate({'opacity': '1'}, 250);
中使用 animate我会使用带有回调的 fadeIn 和 fadeOut 函数。然后您确定淡入动画在淡出结束后开始
Instead of using animate in
$(this).find("ul").animate({'opacity': '1'}, 250);
I would have used fadeIn and fadeOut functions with callbacks. Then you're sure your fadeIn animation begins after the fadeout ends