jQuery 中的子菜单
我正在用 jquery 制作一个菜单。你可以看到在 http://mywash.dk/testtest/index.html
的子菜单从一开始就显示(在“Hvordan”下方)是活动菜单,因此应该始终显示它(除非您将鼠标悬停在另一个菜单项上,在这种情况下,应该显示另一个子菜单。
它工作正常,但有 1 个烦人的错误。当您将鼠标悬停时 在显示应有的菜单之前快速显示和隐藏活动菜单,
Hvad”到“Hvem” ,
非常感谢您的帮助!
从“
$(document).ready(function() {
$('#menu > ul > li:not(.inpath) ul').hide();
$('#menu .inpath ul').show();
$('#menu > ul > li:not(.inpath)').hover(
function() {
$('ul', this).show("slide", { direction: "left" }, 400);
if($('#menu li.inpath ul').is(':visible') && $(this).not('#menu ul li')){
$('#menu li.inpath ul').hide("slide", { direction: "left" }, 400);
}
}, function() {
$('ul', this).hide("slide", { direction: "left" }, 400);
if($('#menu li.inpath ul').is(':hidden') && $(this).not('#menu ul li')){
$('#menu li.inpath ul').show("slide", { direction: "left" }, 400);
}
});
});
它
<div id="menu">
<ul>
<li class="test"><a href="">Hvem</a>
<ul>
<li class="first-item"><a href="index.html">Submenu</a></li>
</ul>
</li>
<li class="test"><a href="">Hvad</a>
<ul>
<li class="first-item"><a href="index.html">Produkter</a></li>
<li class="activeitem"><a href="cases.html" >Leveringer</a></li>
</ul>
</li>
<li class="inpath test"><a href="">Hvordan</a>
<ul>
<li class="first-item"><a href="">Reklame</a></li>
<li><a href="">PR</a></li>
<li><a href="">Websites</a></li>
<li><a href="">Illustrationer</a></li>
</ul>
</li>
<li class="last-item test"><a href="">Sådan!</a></li>
</ul>
<div class="clear"><!--clearfix--></div>
</div>
I'm making a menu in jquery. You can see it at http://mywash.dk/testtest/index.html
The submenu that is shown from the start (below 'Hvordan') is the active menu so it should always be shown (unless you hover on another menu-item in which case another submenu should show.
It's working okay but has 1 annoying bug. When you hover from 'Hvad' to 'Hvem' it quickly display and hides the active menu before showing the menu it is supposed to.
Any idea why this is the case?
Thanks so much for your help!
Jquery:
$(document).ready(function() {
$('#menu > ul > li:not(.inpath) ul').hide();
$('#menu .inpath ul').show();
$('#menu > ul > li:not(.inpath)').hover(
function() {
$('ul', this).show("slide", { direction: "left" }, 400);
if($('#menu li.inpath ul').is(':visible') && $(this).not('#menu ul li')){
$('#menu li.inpath ul').hide("slide", { direction: "left" }, 400);
}
}, function() {
$('ul', this).hide("slide", { direction: "left" }, 400);
if($('#menu li.inpath ul').is(':hidden') && $(this).not('#menu ul li')){
$('#menu li.inpath ul').show("slide", { direction: "left" }, 400);
}
});
});
Html:
<div id="menu">
<ul>
<li class="test"><a href="">Hvem</a>
<ul>
<li class="first-item"><a href="index.html">Submenu</a></li>
</ul>
</li>
<li class="test"><a href="">Hvad</a>
<ul>
<li class="first-item"><a href="index.html">Produkter</a></li>
<li class="activeitem"><a href="cases.html" >Leveringer</a></li>
</ul>
</li>
<li class="inpath test"><a href="">Hvordan</a>
<ul>
<li class="first-item"><a href="">Reklame</a></li>
<li><a href="">PR</a></li>
<li><a href="">Websites</a></li>
<li><a href="">Illustrationer</a></li>
</ul>
</li>
<li class="last-item test"><a href="">Sådan!</a></li>
</ul>
<div class="clear"><!--clearfix--></div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,它确实如此,你告诉它什么。分解:
主导航可见,子导航隐藏
鼠标进入子导航:滚动离开主导航,滚动子导航
鼠标再次离开子项:滚动离开子项,在主项中滚动
鼠标进入另一个子项:滚动子项,滚动离开主项。 但是在此事件发生时,3. 的动画尚未完成。因此,新的动画只是堆叠在当前排队的顶部。
如何修复?
您需要取消主导航上的当前动画。让您的悬停函数看起来像这样:
或者您在输入时查询主队列的长度:
基本思想如下 在 jQuery 文档中,并在这个答案中进行了解释< /a>,也是。基本上,每次将动画放在 jQuery 对象上时,该动画都会排队并在所有当前运行的动画之后执行。
因此,如果鼠标进入导航点,您必须通过查看其队列长度来检查主导航现在是否处于运动状态。如果有堆积的动画,可以调用
.stop(true, true)
清空队列,并将元素设置为所有动画都已完成的状态(双真
)。然后你可以隐藏它,并且没有动画会妨碍你。如果它不在运动中,即如果它有一个空队列,并且并且如果它可见,(并且仅在这种情况下),您必须将其滑出视图。
Well, it does, what you tell it. Break it down:
The main nav is visible, child navs are hidden
Mouse enters a child: Scroll away main, scroll in child
Mouse leaves child again: Scroll away child, scroll in main
Mouse enters another child: Scroll in child, scroll away main. But at the time of this event, the animation of 3. hasn't finished yet. So, the new animations just stack on top of the currently queued.
How to fix?
You need to cancel the current animations on the main nav. Let your hover-in function look like this:
or you query the length of the main queue on entering:
The basic idea is laid out in the jQuery docs and explained in this SO answer, too. Basically, every time you put an animation on a jQuery object, the animation is queued and executed after all currently running animations.
So if the mouse enters a navigation point, you have to check, if the main navigation is in motion right now by looking at its queue length. If there are animations piled up, you can call
.stop(true, true)
to empty the queue and set the element in a state as if all the animations would have finished (the double-true
). Then you can hide it, and no animation will come in your way.If it is not in motion, i.e., if it has an empty queue, and if it is visible, (and only in this case,) you have to slide it out of view.