将 setTimeout 添加到现有 jQuery 脚本的语法问题

发布于 2024-09-14 06:32:15 字数 662 浏览 9 评论 0原文

我想在以下代码中添加 setTimeout ,以便在执行 fadeOut 效果之前有一个短暂的暂停。

$(document).ready(function() {  
    $('#menu li').hover(
        function() {
            $('ul', this).slideDown(50);
        }, 
        function() {
            $('ul', this).fadeOut(100);
        }
    );
});

这就是我正在尝试的,但我猜语法一定是错误的:

$(document).ready(function() {  
    $('#menu li').hover(
        function() {
            $('ul', this).slideDown(50);
        },
        function() {
            setTimeout(function() {
                $('ul,' this).fadeOut(100);
            });
        }
    );
});

抱歉,如果这是一个愚蠢的问题。我是 jQuery 的初学者。

I want to add a setTimeout to the following code so that there's a short pause before the fadeOut effect executes.

$(document).ready(function() {  
    $('#menu li').hover(
        function() {
            $('ul', this).slideDown(50);
        }, 
        function() {
            $('ul', this).fadeOut(100);
        }
    );
});

This is what I'm trying, but I'm guessing the syntax must be wrong:

$(document).ready(function() {  
    $('#menu li').hover(
        function() {
            $('ul', this).slideDown(50);
        },
        function() {
            setTimeout(function() {
                $('ul,' this).fadeOut(100);
            });
        }
    );
});

Sorry if this is a dumb question. I'm a beginner with jQuery.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

堇色安年 2024-09-21 06:32:15

this 的含义在 setTimeout() 中是不同的。您需要在变量中引用所需的 this 或首先获取 ul 并引用它。

var th = this;
setTimeout(function() {
    $('ul', th).fadeOut(100);
});

或者

var $ul = $('ul',this);
setTimeout(function() {
    $ul.fadeOut(100);
});

The meaning of this is different within a setTimeout(). You need to reference the desired this in a variable or get the ul first and reference that.

var th = this;
setTimeout(function() {
    $('ul', th).fadeOut(100);
});

or

var $ul = $('ul',this);
setTimeout(function() {
    $ul.fadeOut(100);
});
我不咬妳我踢妳 2024-09-21 06:32:15

您可能还需要使用 clearTimeout() (如果您快速将鼠标悬停在/退出),类似这样的操作会起作用:

$(function() {  
  $('#menu li').hover(function() {
    clearTimeout($.data(this, 'timer'));
    $('ul', this).slideDown(50);
  }, function() {
    $.data(this, 'timer', setTimeout($.proxy(function() {
      $('ul,' this).fadeOut(100);
    }, this), 400));
  });
});

这使用 $.data(),目前有400ms的延迟,只需相应调整即可。

You could also need to clear the timeout when going over it using clearTimeout() (in case you hover in/out fast), something like this would work:

$(function() {  
  $('#menu li').hover(function() {
    clearTimeout($.data(this, 'timer'));
    $('ul', this).slideDown(50);
  }, function() {
    $.data(this, 'timer', setTimeout($.proxy(function() {
      $('ul,' this).fadeOut(100);
    }, this), 400));
  });
});

This stores/retrieves the timer ID using $.data(), and currently has a 400ms delay, just adjust accordingly.

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