在鼠标悬停时向jquery事件添加延迟 [英] Adding delay to jquery event on mouseover

查看:118
本文介绍了在鼠标悬停时向jquery事件添加延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为孩子的鼠标悬停事件添加一个简单的延迟,并遇到一些困难. (仍在学习!)

I am trying to add a simple delay to a mouseover event of a child and having difficulties. (Still learning!)

这使我能够在延迟后显示弹出窗口,但同时显示所有弹出窗口:

This enables me to show the popup after a delay, but shows all of them simultaneously:

onmouseover='setTimeout(function() { $(\".skinnyPopup\").show(); }, 600)'

,这可以立即显示我想要的弹出窗口:

and this works to show only the popup I want with no delay:

onmouseover='$(this).children(\".skinnyPopup\").show()'

但组合不能:

onmouseover='setTimeout(function() { $(this).children(\".skinnyPopup\").show(); }, 600)'

任何帮助将不胜感激.谢谢!

Any help would be appreciated. Thanks!

推荐答案

您需要定义this在执行时是什么,这样可以正常工作:

You need to define what this is when it executes, something like this would work:

setTimeout($.proxy(function() { $(this).children(".skinnyPopup").show(); }, this), 600)

或者只需使用 .delay() ,如下所示:

Or just use .delay(), like this:

$(this).children(".skinnyPopup").delay(600).show(0);

以上两种方法都是快速修复,建议您不要使用内联处理程序,并查看不引人注目方法(请参见 Russ Cam 回答(出于某些重要原因),例如:

Both of the above are quick fixes, I suggest you move away from inline handlers and check out an unobtrusive method (see this answer by Russ Cam for some great reasons), for example:

$(function() {
  $('selector').mouseover(function() {
    $(this).children(".skinnyPopup").delay(600).show(0);
  });
});

这篇关于在鼠标悬停时向jquery事件添加延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆