I am needing to add a delay to the mouseover event of my dropdown menu so that if someone mouses over the menu going to another link on page the menu will not drop down instantly. Thanks for your help.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
- jQuery add and remove delay
You can add a
setTimeout()
to delay theshow()
, and on hover out clear the timeout so that if the user moves the mouse out before the delay is up it will be cancelled. And you can encapsulate that in your own jQuery plugin:Which you can use like this:
I cobbled that plugin together in a hurry so I'm sure it could be improved, but it seems to work in this updated version of your demo: http://jsfiddle.net/NPVVQ/3/
As far as explaining how my code works: the
.each()
loops through all the elements in the jQuery object that the function is called on. For each element a hover handler is created that usessetTimeout()
to delay calling the callback function provided - if the mouseleave occurs before the time is up this timeout is cleared so that theinCallback
is not called. The.call()
method is used oninCallback
andoutCallback
to set the right value forthis
.You can use CSS3 Animation or Transition.
http://www.css3maker.com/css3-animation.html
http://www.css3maker.com/css3-transition.html
Or you can handle timeouts in Javascript, I think a easy way is to attach a
setTimeout
beforeshow();
and useclearTimeout
on mouseleave eventI updated the code now your menu comes after 1/2 sec. Demo
This is enough to delay the animation....of show() in jquery.
I just used another div to pause the animation you can see it.