创建下拉菜单时如何使用 .hover 保持菜单可见
我正在制作一个下拉(导航)菜单。当用户将鼠标悬停在“DROP”按钮上时,另一个名为“MENU”的 div 会淡入其下方,这将是菜单。我如何保持 div“MENU”显示,因为当用户将鼠标从按钮上移开时,它就会消失。这是 jQuery 代码:
$("#DROP").hover(
function () {
$('#MENU').fadeIn('fast');
},
function () {
$('#MENU').fadeOut('fast');
}
);
当鼠标悬停在“菜单”上方时,如何保持“菜单”显示?当鼠标离开它时,就会消失。同时,确保如果用户没有将鼠标放在“菜单”上,“菜单”就会消失。
I'm making a drop down (nav) menu. When a user hovers over the button "DROP" another div called "MENU" fades in under it, which will be the menu. How would I keep the div "MENU" shown because when the user moves the mouse off the button it disappears. Here's the jQuery code:
$("#DROP").hover(
function () {
$('#MENU').fadeIn('fast');
},
function () {
$('#MENU').fadeOut('fast');
}
);
How would I keep the "MENU" shown when the mouse is over it? THEN fade, when the mouse goes off it. And at the same time, make sure the "MENU" fades if the user doesn't put the mouse over it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也添加此代码,它应该可以解决问题。
Add this code as well, it should do the trick.
您应该使#MENU 成为#DROP 的子元素,这样在光标离开#DROP 和#MENU 之前,mouseout 事件不会触发。
有关示例,请参阅 http://jsfiddle.net/jAHs2/2/。
另一种选择是使用 setTimeout/clearTimeout 的组合来调用 fadeOut 函数,这样您就可以在用户将鼠标悬停在子菜单上时取消它。
You should make #MENU a child element of #DROP, so the mouseout event won't fire until the cursor has left both #DROP and #MENU.
See http://jsfiddle.net/jAHs2/2/ for an example.
Another alternative could be to use a combination of setTimeout/clearTimeout to call the fadeOut function so you can cancel it when the user hovers the submenu.
尝试再订阅一个这样的功能,
Try subscribing to one more function like this,
我相信一种解决方案是为淡出效果添加延迟。 1-2秒应该足够长。
I believe one solution would be to add a delay to the fade-out effect. 1-2 Seconds should be long enough.