js自定义延迟执行函数

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style type="text/css">
        .test div{ width: 500px;height: 500px;}
        #aa{ height: auto;}
    </style>
    <script src="Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        (function ($, window) {
            var $window = $(window);
            $.fn.lazyevent = function (options) {
                var elements = this;
                var settings = {
                    threshold: 0,
                    event: "scroll",
                    container: window,
                    skip_invisible: true,
                    fn: null,
                    data: null
                };
                function update() {
                    elements.each(function () {
                        var $this = $(this);
                        if (settings.skip_invisible && !$this.is(":visible")) {
                            return;
                        }
                        if (inviewport(this, settings)) {
                            $this.trigger("fire");
                        }
                    });

                }
                if (options) {
                    $.extend(settings, options);
                }
                var $container = (settings.container === undefined || settings.container === window) ? $window : $(settings.container);

                if (0 === settings.event.indexOf("scroll")) {
                    $container.bind(settings.event, function (event) { return update(); });
                }
                this.each(function () {
                    var self = this;
                    var $self = $(self);
                    self.fired = false;
                    $self.one("fire", function () {
                        if (!this.fired) {
                            if (settings.fn) {
                                settings.fn.call(self, settings.data);
                            }
                            self.fired = true;
                            var temp = $.grep(elements, function (element) { return !element.fired; });
                            elements = $(temp);
                        }
                    });

                    if (0 !== settings.event.indexOf("scroll")) {
                        $self.bind(settings.event, function (event) {
                            if (!self.fired) {
                                $self.trigger("fire");
                            }
                        });
                    }
                });

                $window.bind("resize", function (event) {
                    update();
                });

                var belowthefold = function (element, settings) {
                    var fold;

                    if (settings.container === undefined || settings.container === window) {
                        fold = $window.height() + $window.scrollTop();
                    } else {
                        fold = $container.offset().top + $container.height();
                    }
                    return fold <= $(element).offset().top - settings.threshold;
                };

                var rightoffold = function (element, settings) {
                    var fold;

                    if (settings.container === undefined || settings.container === window) {
                        fold = $window.width() + $window.scrollLeft();
                    } else {
                        fold = $container.offset().left + $container.width();
                    }

                    return fold <= $(element).offset().left - settings.threshold;
                };
                var abovethetop = function (element, settings) {
                    var fold;

                    if (settings.container === undefined || settings.container === window) {
                        fold = $window.scrollTop();
                    } else {
                        fold = $container.offset().top;
                    }

                    return fold >= $(element).offset().top + settings.threshold + $(element).height();
                };

                var leftofbegin = function (element, settings) {
                    var fold;

                    if (settings.container === undefined || settings.container === window) {
                        fold = $window.scrollLeft();
                    } else {
                        fold = $container.offset().left;
                    }

                    return fold >= $(element).offset().left + settings.threshold + $(element).width();
                };
                var inviewport = function (element, settings) {
                    return !abovethetop(element, settings) && !leftofbegin(element, settings) &&
                        !belowthefold(element, settings) && !rightoffold(element, settings);
                };
                update();
                return this;
            };

        })(jQuery, window);
    </script>
    <script type="text/javascript">
        $(function () {
            $("#aa").lazyevent({
                data: "hello word",
                fn: function (data) {
                    var html = $(this).html();
                    alert(data + ":" + html);
                }
            });
        });
    </script>

</head>
<body>
    <div style="height: 200px">
    </div>
    <div class="test">
        <div style="background-color: Green" >Green</div>
        <div style="background-color: Lime">Lime</div>
        <div style="background-color: Maroon; display:none">Maroon</div>
        <div style="background-color: Olive">Olive</div>
        <div style="background-color: Red; display: none">Red</div>
        <div style="background-color: ButtonFace">ButtonFace</div>
        <div style="background-color: Aqua">Aqua</div>
        <div style="background-color: AppWorkspace; height:auto;" id="aa" >AppWorkspace</div>
    </div>
</body>
</html>

压缩后的js代码:

    <script type="text/javascript">
        (function (a, c) { var b = a(c); a.fn.lazyevent = function (j) { var g = this; var l = { threshold: 0, event: "scroll", container: c, skip_invisible: true, fn: null, data: null }; function m() { var n = 0; g.each(function () { var o = a(this); if (l.skip_invisible && !o.is(":visible")) { return } if (h(this, l)) { o.trigger("fire") } }) } if (j) { a.extend(l, j) } var d = (l.container === undefined || l.container === c) ? b : a(l.container); if (0 === l.event.indexOf("scroll")) { d.bind(l.event, function (n) { return m() }) } this.each(function () { var o = this; var n = a(o); o.fired = false; n.one("fire", function () { if (!this.fired) { if (l.fn) { l.fn.call(o, l.data) } o.fired = true; var p = a.grep(g, function (q) { return !q.fired }); g = a(p) } }); if (0 !== l.event.indexOf("scroll")) { n.bind(l.event, function (p) { if (!o.fired) { n.trigger("fire") } }) } }); b.bind("resize", function (n) { m() }); var f = function (n, p) { var o; if (p.container === undefined || p.container === c) { o = b.height() + b.scrollTop() } else { o = d.offset().top + d.height() } return o <= a(n).offset().top - p.threshold }; var k = function (n, p) { var o; if (p.container === undefined || p.container === c) { o = b.width() + b.scrollLeft() } else { o = d.offset().left + d.width() } return o <= a(n).offset().left - p.threshold }; var e = function (n, p) { var o; if (p.container === undefined || p.container === c) { o = b.scrollTop() } else { o = d.offset().top } return o >= a(n).offset().top + p.threshold + a(n).height() }; var i = function (n, p) { var o; if (p.container === undefined || p.container === c) { o = b.scrollLeft() } else { o = d.offset().left } return o >= a(n).offset().left + p.threshold + a(n).width() }; var h = function (n, o) { return !e(n, o) && !i(n, o) && !f(n, o) && !k(n, o) }; m(); return this } })(jQuery, window);
    </script>
时间: 2024-10-17 22:23:38

js自定义延迟执行函数的相关文章

js延迟执行函数

在js中,延迟执行函数有两种:setTimeout和setInterval setTimeout("test()","2000"); //2000毫秒后执行test()函数,只执行一次. setInterval("test()","2000"); //每隔2000毫秒执行一次test()函数,执行无数次. var interval = window.setInterval("test()","20

IOS 关于取消延迟执行函数的种种。performSelector与cancelPreviousPerformRequestsWithTarget

本文非本人撰写 [cpp] view plaincopy @interface NSObject (NSDelayedPerforming) - (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray *)modes; - (void)performSelector:(SEL)aSelector withObject:(id)a

js中自执行函数(function(){})()和(function(){}())区别

方式一,调用函数,得到返回值.强制函数直接量执行再返回一个引用,引用在去调用执行方式二,调用函数,得到返回值.强制运算符使函数调用执行(function(){})(); 是 把函数当作表达式解析,然后执行解析后的函数相当于 var a = function(){}; a(); a得到的是函数(function(){}()); 是把函数表达式和执行当作语句直接执行.相当于 var a = function(){}(); a得到的是结果最终结果是一样的.()只是起了 自执行的作用和 () 一样的还有

sqlServer数据库定时延迟执行函数

while 1>0 --循环begin update T_D_CHANNEL set R_DAT_VAL=DateName(second,GetDate()) --获取当前系统时间的秒数 where dat_kd_id = 'PU_PWH' or dat_kd_id = 'PU_CH_FQC' or dat_kd_id='PU_WK_FQC' or dat_kd_id='RS_TIME' or dat_kd_id='FL_Q_FD' or dat_kd_id='FL_R_RG' or dat_k

js立即执行函数应用--事件绑定

js中立即执行函数的应用:应用到事件绑定上. 少说多做,直接运行代码(代码中有注释): 1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta charset="UTF-8" /> 5 <meta http-equiv="X-UA-Compatible" content="ie=edge" /> 6 <titl

underscore.js中的节流函数debounce及trottle

函数节流   throttle and debounce的相关总结及想法 一开始函数节流的使用场景是:放止一个按钮多次点击多次触发一个功能函数,所以做了一个clearTimeout setTimeout函数 clearTimeout(cancelTimer); cancelTimer =setTimeout(function(){ switchControl.switchAciontFactory(view, conf); },300) 代码的意思就不做多说了,实际上我无意间实现了一个debou

JS 自定义sleep以及Ajax 自执行函数

<script src="/static/js/jquery-2.1.1.min.js"></script> <script> function sleep(numberMillis) { var now = new Date(); var exitTime = now.getTime() + numberMillis; while (true) { now = new Date(); if (now.getTime() > exitTime)

在js自定义函数中使用$(event.target)代替$(this)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <ti

学习js函数--自执行函数

我在写代码时候经常会在tpl的<script>里写类似的代码: $(function(){ alert("我好饿"); }); 刚开始的时候只知道写了它不需要调用,直接执行,就这样依葫芦画瓢,我写了很多代码.说道这,还要说说这货的加载顺序,如果把代码直接写到script标签里,当页面加载完这个script标签就会执行里边的代码了.如果在这代码里用到了未加载的dom或者调用了未加载的方法,是会报错的.言归正传,这个函数其实就是自执行函数,很多人会比较专业地称为"立即