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

javascript - How to add a delay to CSS Vertical Dropdown Menu - Stack Overflow

programmeradmin2浏览0评论

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.

/

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.

http://jsfiddle/cgagliardi/NPVVQ/

Share Improve this question asked May 29, 2012 at 4:01 RodneyRodney 6241 gold badge11 silver badges27 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You can add a setTimeout() to delay the show(), 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:

jQuery.fn.hoverWithDelay = function(inCallback,outCallback,delay) {
    this.each(function(i,el) {
        var timer;
        $(this).hover(function(){
           timer = setTimeout(function(){
              timer = null;
              inCallback.call(el);
           }, delay);
        },function() {
           if (timer) {
              clearTimeout(timer);
              timer = null;
           } else
              outCallback.call(el);
        });
    });
};

Which you can use like this:

$('ul.top-level li').hoverWithDelay(function() {
    $(this).find('ul').show();
}, function() {
    $(this).find('ul').fadeOut('fast', closeMenuIfOut);
}, 500);

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/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 uses setTimeout() to delay calling the callback function provided - if the mouseleave occurs before the time is up this timeout is cleared so that the inCallback is not called. The .call() method is used on inCallback and outCallback to set the right value for this.

You can use CSS3 Animation or Transition.

http://www.css3maker./css3-animation.html

http://www.css3maker./css3-transition.html

Or you can handle timeouts in Javascript, I think a easy way is to attach a setTimeout before show(); and use clearTimeout on mouseleave event

I updated the code now your menu es after 1/2 sec. Demo

$('#navigation').show(2000);

This is enough to delay the animation....of show() in jquery.

I just used another div to pause the animation you can see it.

发布评论

评论列表(0)

  1. 暂无评论