最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding delay to jquery event on mouseover - Stack Overflow

programmeradmin10浏览0评论

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()'

but the bination does not:

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

Any help would be appreciated. Thanks!

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()'

but the bination does not:

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

Any help would be appreciated. Thanks!

Share Improve this question edited Sep 7, 2010 at 18:47 Eli Courtwright 193k68 gold badges223 silver badges257 bronze badges asked Sep 7, 2010 at 18:44 TLKTLK 1,7701 gold badge17 silver badges33 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

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

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

Or just use .delay(), like this:

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

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);
  });
});

It's because this is bound to the global context, not the element. Use something like the following instead:

// put this in your document head -- replace element with a selector for the elements you want
$(function () {
    $(element).bind("mouseover", function () {
       var e = $(this);
       setTimeout(function () { e.children(".skinnyPopup").show(); }, 600);
    });
});

If you're adamant about inline event handlers, the following should also work:

onmouseover='var self = this; setTimeout(function() { $(self).children(\".skinnyPopup\").show(); }, 600)'
发布评论

评论列表(0)

  1. 暂无评论