IT技术博客大学习 共学习 共进步

jQuery延时绑定事件(lazy-bind)

idea's blog 2011-06-01 13:28:03 浏览 5,441 次

有个延时绑定事件的需求, 如等待鼠标停留在某图片上面一段时间之后才展示浮动层, 以避免鼠标滑过屏幕时一片乱闪. 一时找不到合适的插件, 所以自己写了个.

// 定义
(function($){
    $.fn.lazybind = function(event, fn, timeout, abort){
        var timer = null;
        $(this).bind(event, function(){
            timer = setTimeout(fn, timeout);
        });
        if(abort == undefined){
            return;
        }
        $(this).bind(abort, function(){
            if(timer != null){
                clearTimeout(timer);
            }
        });
    };
})(jQuery);

// 使用
$('#my_img').lazybind(
    'mouseover',
    function(){
        alert(1);
    },
    240,
    'mouseout');

建议继续学习

  1. JQuery实现Excel表格呈现 (阅读 48,164)
  2. 分享一个JQUERY颜色选择插件 (阅读 14,062)
  3. jQuery插件---轻量级的弹出窗口wBox. (阅读 10,624)
  4. 10个强大的Ajax jQuery文件上传程序 (阅读 8,722)
  5. jQuery性能优化指南 (阅读 8,645)
  6. jQuery的data()方法 (阅读 8,504)
  7. jQuery Color Animations颜色动画插件 (阅读 8,344)
  8. 精于图片处理的10款jQuery插件 (阅读 7,261)
  9. 配合jquery实现异步加载页面元素 (阅读 6,284)
  10. jQuery中getJSON跨域原理详解 (阅读 6,263)