下拉悬停问题(jQuery)
我制作了一个简单的下拉菜单,效果很好,但我想在按钮和下拉菜单之间(底部按钮/链接和顶部下拉菜单之间)添加一些空间。
问题是,如果我不在按钮和下拉菜单之间添加空间,它工作正常,但是一旦我在它们之间添加空间,一旦我离开按钮,下拉菜单就会返回隐藏状态。
我可以使 li 在悬停时更高,但是 li 具有附加的样式,所以我不能对此做太多事情(a 元素也附加了固定的样式)。
那么,如果我将鼠标悬停在其父 li 上,如果下拉菜单之间有空间,我该如何保持下拉菜单处于活动状态呢?
jQuery 代码
$('#sub-menu li').hover(function(){
$(this).children('ul').stop(true,true).fadeIn(100);
},function(){
$(this).children('ul').stop(true,true).fadeOut(100);
});
基本 html 代码
<div>
<ul>
<li><a href="#">link</a>
<ul>
<li>dropdown menu</li>
<li>dropdown menu</li>
<li>dropdown menu</li>
</ul>
</li>
</ul>
</div>
I have made a simple dropdown menu, this works fine but i want to add some space between the button and the dropdown menu(between bottom button/link and top dropdown menu).
The issue, if i dont add space between the button and dropdown menu it works fine, but once that i add space between them the dropdown menu goes back to hide once i leave the button.
I can make the li taller on hover but the li has style attached to it so i cant do much with this(also the a elements has fixed style attached to it).
So how can i keep the dropdown menu alive if its got space between it if i hover its parent li?
The jQuery code
$('#sub-menu li').hover(function(){
$(this).children('ul').stop(true,true).fadeIn(100);
},function(){
$(this).children('ul').stop(true,true).fadeOut(100);
});
Basic html code
<div>
<ul>
<li><a href="#">link</a>
<ul>
<li>dropdown menu</li>
<li>dropdown menu</li>
<li>dropdown menu</li>
</ul>
</li>
</ul>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
执行此操作的典型方法是设置一个计时器,以在父级的 mouseleave 事件上关闭子菜单,但如果在子菜单上进行 mouseenter,则取消计时器。将此计时器设置为 150-250 毫秒通常可以为适度缓慢移动的鼠标提供充足的时间来保持预期行为,但当菜单要关闭时不会感觉太迟缓。
未经测试,更像是伪代码,只是为了展示一般方法:
The typical way to do this is to set up a timer to close the submenu on the parent's mouseleave event, but cancel the timer if you mouseenter on the submenu. Setting this timer for 150-250ms usually gives plenty of time for a moderately slow-moving mouse to preserve the intended behavior, but doesn't feel too laggy when the menu is intended to close.
Not tested, more like pseudocode, just to show the general approach:
我想我明白了,尽管 css 在这里会很有帮助。我相信您的子
ul
有position:abosolute
并且您在父li
上有hover
。当两者在一起时,您可以将鼠标从一个移动到另一个,而无需离开li
,但是一旦您在两者之间留出间隙,请将鼠标从链接移动到ul< /code> 菜单离开父
li
导致mouseleave
触发。我处理这个问题的方式可能如下。不要重新定位子
ul
,而是将该ul
包装在 div 中。在 div 上放置一个padding-top
,并提供所需的空间。然后移动所有定位 css 和 jquery 的东西来显示/隐藏/定位 div。这样,从链接到菜单ul
就不会碰到空白区域并隐藏,而是会移动到透明 div 中,该 div 仍然是父li
的一部分并且菜单不会隐藏。希望有帮助,再次 css 将有助于准确了解您在做什么。
编辑:创建了一个小提琴来演示这个想法。第一个链接显示问题,第二个链接显示修复,并用边框显示实际发生的情况,第三个链接是实际修复。
http://jsfiddle.net/fTmLh/
I think I understand, although the css would be very helpful here. I believe your child
ul
hasposition:abosolute
and you have ahover
on the parentli
. When the two are together, you can move your mouse from one to the other without leaving theli
but once you put a gap between the two, moving your mouse from the link to theul
menu leaves the parentli
causing themouseleave
to fire.The way I would probably deal with this would be as follows. Instead of re-positioning the child
ul
, wrap thatul
in a div. Put apadding-top
on the div with as much space as you want. Then move all of the positioning css and jquery stuff to show/hide/position the div instead. In this way, going from the link to the menuul
would not hit empty space and hide, it would instead move into the transparent div which is still part of the parentli
and the menu would not hide.Hope that helps, again css would be helpful to see exactly what you're doing.
EDIT: Created a fiddle to demonstrate the idea. The first link shows the problem, 2nd shows the fix, with a border to show what's actually happening and the 3rd is the actual fix.
http://jsfiddle.net/fTmLh/
简单的答案是仅增加顶级链接的
标记中的底部填充。专门为他们创建一个新类或动态添加它,如下所示:
The simple answer is to just increase the bottom padding in your
<LI>
tags for the top level links. create a new class specifically for them or add it on-the-fly, like this: