zoukankan      html  css  js  c++  java
  • ☀【jQuery插件】DOM 延迟渲染

    test.html

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            .invisible {
                visibility: hidden;
            }
            #box1 {
                height: 1000px;
                background: #F00;
            }
            #box2 {
                height: 100px;
                background: #0F0;
            }
        </style>
    </head>
    <body>
        <div id="box1">box1</div>
        <textarea class="jq-datalazyload invisible">
            <script>
                alert('222');
            </script>
            <div id="box2">box2</div>
        </textarea>
        <script src="jquery-1.6.4.min.js"></script>
        <script src="datalazyload.js"></script>
        <script>
            var lazy = new $.Datalazyload()
            lazy.init()
        </script>
    </body>
    </html>

    datalazyload.js

    /**
     * Created with JetBrains PhpStorm.
     * User: Administrator
     * Date: 13-6-18
     * Time: 下午2:13
     * To change this template use File | Settings | File Templates.
     */
    (function($) {
        var AREA_DATA_CLS = "jq-datalazyload";
    
        function Datalazyload(config) {
            var defaultConfig = {
            };
            this.setting = $.extend(defaultConfig, config || {});
        }
    
        $.extend(
            Datalazyload.prototype, {
                init: function() {
                    var self = this;
                    self._getAreaList();
                    for (var i = 0; i < self.areaList.length; i++) {
                        self._loadAreaItem(self.areaList[i]);
                    }
                    $(window).bind("scroll resize", function() {
                        self._getAreaList();
                        for(var i = 0; i < self.areaList.length; i++){
                            self._loadAreaItem(self.areaList[i]);
                        }
                    });
                },
                _getAreaList: function() {
                    var self = this;
                    var areaList = [];
                    areaList = $("textarea").filter("." + AREA_DATA_CLS);
                    self.areaList = areaList;
                    if (areaList.length == 0) {
                        $(window).unbind("scroll resize");
                    }
                },
                _replaceArea: function(area) {
                    var self = this;
                    var _area = $(area);
                    var _html = _area.val();
                    _area.removeClass(AREA_DATA_CLS);
                    $(_html).insertBefore(_area);
                    _area.hide();
                },
                _loadAreaItem: function(area) {
                    var self = this;
                    var top;
                    var viewHeight = $(window).height();
                    var scrollTop = $(window).scrollTop();
                    if ($(area).offset().top != 0) {
                        top = $(area).offset().top;
                    } else {
                        top = $(area).parent().offset().top;
                    }
                    if (top <= viewHeight + scrollTop) {
                        self._replaceArea(area)
                    }
                }
            }
        );
        $.Datalazyload = Datalazyload;
    })(jQuery);
  • 相关阅读:
    范畴定义
    泛函编程(0)-什么是泛函编程
    函数式语言的特性
    理解函数式编程
    未阅归档
    monad-本质解释- a monad is a design pattern--monad与泛型相关
    打印管理系统
    函数式JS: 原来promise是这样的monad
    Promise是Monad吗?
    Scala和范畴论 -- 对Monad的一点认识
  • 原文地址:https://www.cnblogs.com/jzm17173/p/3407411.html
Copyright © 2011-2022 走看看