jQuery – 鼠標經過(hover)事件的延時處理


說到延時,離不開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         鼠標移出執行的方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM