当鼠标位于下拉菜单的子菜单中时,如何保持父菜单悬停
大家好,我想知道如何在子菜单中移动鼠标时保持父菜单悬停。
我是 jQuery 的初学者,我希望你能帮助我提供一些提示/建议。
jQuery 代码
// Navigation Slide //
var navHover = function () {
$("#S" + this.id).animate({top: '-40px'}, 300, 'swing')
$(this).animate({paddingTop: '30px'}, 300, 'swing').animate({paddingTop: '45px'}, 300, 'swing')
$("#I" + this.id).animate({top: '-10px'}, 300, 'swing').animate({top: '0px'}, 300, 'swing')
}
var navRelease = function () {
$("#S" + this.id).animate({top: '-130px'}, 300, 'swing');
}
$('#navInside a.topLevel').hover(navHover, navRelease);
// Dropdown animation
function mainmenu(){
jQuery(" #navInside ul ").css({display: "none"}); // Opera Fix
jQuery(" #navInside li").hover(function(){
jQuery(this).find('ul:first').css({visibility: "visible",display: "none"}).slideDown(500);
},function(){
jQuery(this).find('ul:first').css({visibility: "hidden"});
});
}
jQuery(document).ready(function(){
mainmenu();
});
导航 HTML
<div id="navInside">
<li><a class="topLevel" href="">Home</a></li>
<li><a class="topLevel" href="">Options</a>
<ul>
<li><a href="">Submenu 1</a></li>
<li><a href="">Submenu 2</a></li>
</ul>
</li>
<li><a class="topLevel" href="">Thanks</a></li>
Hi guys i wonder how to keep the parent menu hovered while moving mouse in the submenus.
I'm a beginner in jQuery and I like you to help me with some tip/suggestion.
jQuery Code
// Navigation Slide //
var navHover = function () {
$("#S" + this.id).animate({top: '-40px'}, 300, 'swing')
$(this).animate({paddingTop: '30px'}, 300, 'swing').animate({paddingTop: '45px'}, 300, 'swing')
$("#I" + this.id).animate({top: '-10px'}, 300, 'swing').animate({top: '0px'}, 300, 'swing')
}
var navRelease = function () {
$("#S" + this.id).animate({top: '-130px'}, 300, 'swing');
}
$('#navInside a.topLevel').hover(navHover, navRelease);
// Dropdown animation
function mainmenu(){
jQuery(" #navInside ul ").css({display: "none"}); // Opera Fix
jQuery(" #navInside li").hover(function(){
jQuery(this).find('ul:first').css({visibility: "visible",display: "none"}).slideDown(500);
},function(){
jQuery(this).find('ul:first').css({visibility: "hidden"});
});
}
jQuery(document).ready(function(){
mainmenu();
});
Navigation HTML
<div id="navInside">
<li><a class="topLevel" href="">Home</a></li>
<li><a class="topLevel" href="">Options</a>
<ul>
<li><a href="">Submenu 1</a></li>
<li><a href="">Submenu 2</a></li>
</ul>
</li>
<li><a class="topLevel" href="">Thanks</a></li>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您的顶级悬停在 上元素。移动到子菜单会导致 上触发 mouseleave 事件。因为子菜单元素不是 的子元素,而是
顺便说一句 - 您可以通过在悬停函数中使用 $(this) 来简化 navHover/navRelease 代码。这样你就不需要元素上的特定 ID。您只需找到相对于当前元素的它们。
The problem is that your top-level hover is on the <a> element. Moving to a submenu results in the mouseleave event firing on the <a> element since the submenu elements are not children of the <a>, but of the <li>. Try this instead:
BTW - You can simplify your navHover/navRelease code by using the $(this) within the hover functions. That way you don't need specific ids on the elements. You would just find them relative to the current element.
你根本不需要 JS。以下是 jsFiddle 的解释:
您想要显示一个
ul
仅当您将鼠标悬停在li
上时,它才是li
的子级。这是由 li ul(鼠标悬停状态)和 li:hover ul(鼠标悬停状态)处理的。当您将鼠标悬停在其上时,LI 的高度会发生变化,因为您现在也显示 UL,因此只要您不离开其 (LI) 区域(包括其文本 + UL),它就会保持可见。
如果您想要一些动画,请查看 CSS 过渡。浏览器支持参差不齐,但也许您想要做的事情会得到普遍支持。准确判断区域正在变化的元素上的鼠标悬停/移出事件可能很棘手。我建议仅当您确实需要这样做时才使用 JS。
You don't need JS for this at all. Here's the jsFiddle for the explanation below:
You want to show a
ul
that is the child of anli
only when you're hovering over it. That is handled byli ul
(mouseout state) andli:hover ul
(mouseover state).When you hover over it, the height of the LI changes because you're now displaying the UL as well, so it will stay visible as long as you don't leave its (LIs) area (which includes its text + UL).
If you want some animations, look at CSS transitions. Browser support is spotty, but maybe what you're trying to do will be supported universally. Accurately judging mouseover/out events on elements whose area is changing can be tricky. I'd recommend using JS only if you really need to for something like this.