在具有非嵌套子菜单的 jquery 导航菜单上实现 setTimeout 时出现问题

发布于 2024-12-20 19:28:29 字数 2772 浏览 3 评论 0原文

我有一个带有子菜单的菜单列表,这些子菜单未出于样式目的而嵌套。因此,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜嗑 2024-12-27 19:28:29

这是你的问题吗?

}).mouseleave(function() {
    clearTimeout(timeout);
    var timeout = setTimeout(function() {
    ^^^

Is this your issue?

}).mouseleave(function() {
    clearTimeout(timeout);
    var timeout = setTimeout(function() {
    ^^^
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文