代码之家  ›  专栏  ›  技术社区  ›  DisgruntledGoat

延迟后淡出jquery菜单

  •  2
  • DisgruntledGoat  · 技术社区  · 15 年前

    我正在开发一个jquery下拉菜单,当您悬停在顶级项目上时,该菜单将淡入。我想设置它,这样当你把鼠标移开时,菜单就不会立即消失。我有这个代码:

    $(document).ready(function(){
      $('ul#menu > li').hover(
        // mouseover
        function(){
          $(this).find('>ul').fadeIn('fast');
        },
        // mouseout
        function(){
          setTimeout( function(){
            alert('fadeout');
            $(this).find('>ul').fadeOut('fast')
            }, 1000 );
        }  
      );
    });
    

    过了一会儿,警报就出现了,但菜单没有淡出。

    2 回复  |  直到 15 年前
        1
  •  3
  •   user120242    15 年前

    window.setTimeout(),所以这是指window对象。

    // mouseout
    function(){
      var el=this;
      setTimeout( function(){
        alert('fadeout');
        $(el).find('>ul').fadeOut('fast')
        }, 1000 );
    }  
    
        2
  •  3
  •   karim79    15 年前

    看一看 hoverIntent . 它能让你更好地控制 mouseover / mouseout 按配置排列的事件:

    var config = {    
         sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
         interval: 200, // number = milliseconds for onMouseOver polling interval    
         timeout: 500, // number = milliseconds delay before onMouseOut    
    };
    
    $(document).ready(function(){
      $('ul#menu > li').hoverIntent(
        // mouseover
        function(){
          $(this).find('>ul').fadeIn('fast');
        },
        // mouseout
        function(){
           $(this).find('>ul').fadeOut('fast');
        }  
      );
    });