在具有非嵌套子菜单的 jquery 导航菜单上实现 setTimeout 时出现问题
我有一个带有子菜单的菜单列表,这些子菜单未出于样式目的而嵌套。因此,我使用 mouseenter 和 mouseleave 以及 settimeout 来实现悬停行为。
这是我的 html:
<li class="dropdown"><a href="#">Link 1</a></li>
<div class="subnav">
<ul>
<li><a href="#">Link 1.1</a></li>
<li><a href="#">Link 1.2</a></li>
<li><a href="#">Link 1.3</a></li>
<li><a href="#">Link 1.4</a></li>
</ul>
</div>
<li class="dropdown"><a href="#">Link 2</a></li>
<div class="subnav">
<ul>
<li><a href="#">Link 2.1</a></li>
<li><a href="#">Link 2.2</a></li>
<li><a href="#">Link 2.3</a></li>
<li><a href="#">Link 2.4</a></li>
</ul>
</div>
<li class="dropdown"><a href="#">Link 3</a></li>
<div class="subnav">
<ul>
<li><a href="#">Link 3.1</a></li>
<li><a href="#">Link 3.2</a></li>
<li><a href="#">Link 3.3</a></li>
<li><a href="#">Link 3.4</a></li>
</ul>
</div>
</ul>
</div><!-- End topNav -->
和我的 jquery:
$(document).ready(function(){
var overSubmenuFlag = false;
var timeout;
var subnav;
var offset;
$("li.dropdown").mouseenter(function(){
clearTimeout(timeout);
$(".subnav").hide();
$(this).children('a').addClass("active"); //Add active class to link element
var subnav = $(this).next("div.subnav"); //find navigation box associated with this nav tab
var offset = $(this).position(); //Position subnav relative to nav tab
var leftpos = (offset.left + 15) + "px";
var toppos = (offset.top + 36) + "px";
subnav.css( {
position: 'absolute',
left: leftpos,
top: toppos
});
subnav.show(); //Reveal subnav
}).mouseleave(function() {
clearTimeout(timeout);
var timeout = setTimeout(function() {
if(!overSubmenuFlag) {
var subnav = $("li.dropdown").next("div.subnav");
subnav.hide();
$("li.dropdown").children('a').removeClass("active");
}},100);
});
$(".subnav").mouseenter(function(){
clearTimeout(timeout);
overSubmenuFlag = true;
}).mouseleave(function(){
overSubmenuFlag = false;
$(".subnav").hide();
$("li.dropdown").children('a').removeClass("active");
});
});
一切都按预期工作,除了以下情况:当我将鼠标移出一个导航标题并快速将鼠标悬停在另一个标题上时,与新标题关联的子菜单会短暂出现然后消失。 (在两个导航标题之间缓慢移动鼠标可以正常工作)。在进入新的导航时,如何正确停止与 setTimeout 函数相关的所有操作(并关闭任何打开的子菜单)。标题?非常感谢您的帮助!
I have a menu list with submenus that are not nested for styling purposes. Thus, I'm using mouseenter and mouseleave, together with settimeout, for hovering behavior.
Here's my html:
<li class="dropdown"><a href="#">Link 1</a></li>
<div class="subnav">
<ul>
<li><a href="#">Link 1.1</a></li>
<li><a href="#">Link 1.2</a></li>
<li><a href="#">Link 1.3</a></li>
<li><a href="#">Link 1.4</a></li>
</ul>
</div>
<li class="dropdown"><a href="#">Link 2</a></li>
<div class="subnav">
<ul>
<li><a href="#">Link 2.1</a></li>
<li><a href="#">Link 2.2</a></li>
<li><a href="#">Link 2.3</a></li>
<li><a href="#">Link 2.4</a></li>
</ul>
</div>
<li class="dropdown"><a href="#">Link 3</a></li>
<div class="subnav">
<ul>
<li><a href="#">Link 3.1</a></li>
<li><a href="#">Link 3.2</a></li>
<li><a href="#">Link 3.3</a></li>
<li><a href="#">Link 3.4</a></li>
</ul>
</div>
</ul>
</div><!-- End topNav -->
And my jquery:
$(document).ready(function(){
var overSubmenuFlag = false;
var timeout;
var subnav;
var offset;
$("li.dropdown").mouseenter(function(){
clearTimeout(timeout);
$(".subnav").hide();
$(this).children('a').addClass("active"); //Add active class to link element
var subnav = $(this).next("div.subnav"); //find navigation box associated with this nav tab
var offset = $(this).position(); //Position subnav relative to nav tab
var leftpos = (offset.left + 15) + "px";
var toppos = (offset.top + 36) + "px";
subnav.css( {
position: 'absolute',
left: leftpos,
top: toppos
});
subnav.show(); //Reveal subnav
}).mouseleave(function() {
clearTimeout(timeout);
var timeout = setTimeout(function() {
if(!overSubmenuFlag) {
var subnav = $("li.dropdown").next("div.subnav");
subnav.hide();
$("li.dropdown").children('a').removeClass("active");
}},100);
});
$(".subnav").mouseenter(function(){
clearTimeout(timeout);
overSubmenuFlag = true;
}).mouseleave(function(){
overSubmenuFlag = false;
$(".subnav").hide();
$("li.dropdown").children('a').removeClass("active");
});
});
Everything works as intended, except the following: when I mouseout of one navigation header and mouseover another header quickly, the submenu associated with the new header briefly appears then disappears. (Mousing slowly between two navigation headers works properly). How do I properly stop all actions associated with the setTimeout function (and close any open submenus) upon entering a new nav. header? Many thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是你的问题吗?
Is this your issue?