适用场景如:鼠标随便一滑,无数个下拉菜单出来了,用户可能只是鼠标“打个酱油”罢了。

实现很简单,就是使用setTimeout延迟事件代码的执行。

(function($){
function DelayHover(options) {
var defaults = {
speed : 400, // 延迟时间(毫秒)
onHover : function(el) {}, // 鼠标悬浮
onOut : function(el){} // 鼠标离开
};
this.settings = $.extend({}, defaults, options);
var _this = this, hoverTimer;
_this.bind = function () {
$(_this).hover(function() {
hoverTimer = setTimeout(function(){
_this.settings.onHover(_this);
}, _this.settings.speed);
}, function() {
clearTimeout(hoverTimer);
_this.settings.onOut(_this);
});
};
}
$.fn.delayHover = function(options) {
return this.each(function() {
DelayHover.call(this,options);
this.bind();
});
}
})(jQuery);