2

我正在开发一个 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 );
    }  
  );
});

一秒钟后,警报发生,但菜单没有淡出。

4

2 回答 2

3

window.setTimeout(),所以 this 指的是窗口对象。

// mouseout
function(){
  var el=this;
  setTimeout( function(){
    alert('fadeout');
    $(el).find('>ul').fadeOut('fast')
    }, 1000 );
}  
于 2009-09-22T01:19:53.913 回答
3

看看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');
    }  
  );
});
于 2009-09-22T01:22:12.597 回答