[英]jquery hover to show menu issue
I'm trying to add a hover effect to show/hide a dropdown menu. 我正在尝试添加悬停效果以显示/隐藏下拉菜单。 The problem is when I hover over the link the menu shows but disappears again.
问题是,当我将鼠标悬停在链接上时,菜单显示但又消失了。
$(".hoverli").hover(
function () {
$('ul.file_menu').slideDown('medium');
},
function () {
$('ul.file_menu').slideUp('medium');
}
);
Please take a look at the demo here: fiddle . 请在这里查看演示: 小提琴 。
Can you tell me what I'm missing in the code? 您能告诉我代码中缺少的内容吗?
thanks 谢谢
The reason is, when you are on the .file_menu
it is no more hover
. 原因是,当您位于
.file_menu
,不再hover
。 So, a small change will fix. 因此,将进行较小的更改。
$(document).ready(function () {
$("#button").hover(
function () {
$('ul.file_menu').slideDown('medium');
},
function () {
$('ul.file_menu').slideUp('medium');
}
);
});
发生这种情况是因为ul不在锚标记内,所以当您将ul悬停时,实际上是在触发锚的“ out”事件。
Here's how I'd do it : 这是我的做法:
$(document).ready(function() {
var tim;
$(".hoverli, .file_menu").on({
mouseenter: function() {
clearTimeout(tim);
$('ul.file_menu').slideDown('medium');
},
mouseleave: function() {
tim = setTimeout(function() {
$('ul.file_menu').slideUp('medium');
}, 300);
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.