jQuery鼠標經過(hover)事件的延時處理,具體JS代碼如下:
(function($){
$.fn.hoverDelay = function(options){
var defaults = {
hoverDuring: 200,
outDuring: 200,
hoverEvent: function(){
$.noop();
},
outEvent: function(){
$.noop();
}
};
var sets = $.extend(defaults,options || {});
var hoverTimer, outTimer;
return $(this).each(function(){
$(this).hover(function(){
clearTimeout(outTimer);
hoverTimer = setTimeout(sets.hoverEvent, sets.hoverDuring);
},function(){
clearTimeout(hoverTimer);
outTimer = setTimeout(sets.outEvent, sets.outDuring);
});
});
}
})(jQuery);
hoverDelay方法共四個參數,表示意思如下:
hoverDuring 鼠標經過的延時時間
outDuring 鼠標移出的延時時間
hoverEvent 鼠標經過執行的方法
outEvent 鼠標移出執行的方法
該函數的目的在於讓鼠標經過事件和延時分離的出來,延時以及延遲的清除都已經由此方法解決了。您所要做的,就是設定延時的時間大小,以及相應的鼠標經過或是移除事件即可。舉個簡單的例子吧,如下代碼:
$("#test").hoverDelay({
hoverDuring: 1000,
outDuring: 1000,
hoverEvent: function(){
$("#tm").show();
},
outEvent: function(){
$("#tm").hide();
}
});
以下為更簡潔的一個案例:
$("#test").hoverDelay({
hoverEvent: function(){
alert("經過 我!");
}
});
表示的含義是id為test的元素在鼠標經過後200毫秒後彈出含有“經過 我!”文字字樣的彈出框。
以上就是關於jQuery鼠標經過(hover)事件的延時處理全部內容,希望對大家的學習有所幫助。