JQuery mouseout timeout

☆樱花仙子☆ 提交于 2019-12-24 19:19:05

问题


Similar problems have been dealt with before but I believe mine's slightly different owing to the use of the bind() function. Anyhow...

$('.overlay').bind("mouseenter",function(){
  $(this).fadeTo('slow', 0);
}).bind("mouseleave",function(){                
  setTimeout(function() { 
    $(this).fadeTo('slow', 1);
  }, 2000);
});

I want to fade out the overlay on "mouseenter", but only fade it back in 2000ms after "mouseleave".

I have an additional question: When the .overlay div fades out, I need to be able to click on what's beneath it i.e. I need the div to disappear completely or move down the z-index stack. However, if I try adding this in, the script thinks the mouse has left the .overlay div, so the .overlay fades back in.

For the same reason I can't use fadeOut() and fadeIn().


回答1:


When the timeout fires this won't be what you expect. You can create a closure like this:

            $('.overlay').bind("mouseenter",function(){
                    $(this).fadeTo('slow', 0);
                    }).bind("mouseleave",function(){
                    var $this = $(this);                               
                    setTimeout(function() { 
                            $this.fadeTo('slow', 1);
                            }, 2000);
            });


来源:https://stackoverflow.com/questions/1440298/jquery-mouseout-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!