說到延時,離不開window下的setTimeout方法,本實例的jQuery方法的核心也是setTimeout。代碼不長,完整如下:
(function ($) { $.fn.hoverDelay = function (hoverEvent, outEvent) { var hoverTimer, outTimer; return $(this).each(function () { $(this).hover(function () { var t = this; clearTimeout(outTimer); hoverTimer = setTimeout(function () { hoverEvent.call(t); }, 200); }, function () { var t = this; clearTimeout(hoverTimer); outTimer = setTimeout(function () { outEvent.call(t); }, 200); }); }); } })(jQuery);
基本上都是代碼在撐頁面,說點有用的東西吧。hoverDelay
方法共四個參數,表示意思如下:
hoverDuring 鼠標經過的延時時間
outDuring 鼠標移出的延時時間
hoverEvent 鼠標經過執行的方法
outEvent 鼠標移出執行的方法