首頁  >  文章  >  web前端  >  jQuery實作滑鼠經過事件的延時處理效果_jquery

jQuery實作滑鼠經過事件的延時處理效果_jquery

WBOY
WBOY原創
2016-05-16 15:35:141510瀏覽

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)事件的延時處理全部內容,希望對大家的學習有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn