页面的懒加载

        'lazyQueue': {},
        'isVisiable': function(elem) {
            if (!elem) return false;
            // console.log(($(window).scrollTop()+"::"+($(elem).offset().top+$(elem).outerHeight()))+"::"+(($(window).scrollTop()+$(window).height())+"::"+$(elem).offset().top))
            return ($(window).scrollTop() <= ($(elem).offset().top + $(elem).outerHeight())) && (($(window).scrollTop() + $(window).height()) > $(elem).offset().top);
        },
        'lazyload': function(elem, htmlcode, item, cb) {
            elem.html(htmlcode.html);
            elem.css('display', 'block');
            if (elem && elem.length != 0) {
                var top = elem.offset().top;

                if (this.isVisiable(elem)) {
                    this.showImg(elem);
                    cb && cb.apply(this, arguments);
                    return;
                }
                if (this.lazyQueue[top]) {
                    this.lazyQueue[top].push({
                        'elem': elem,
                        'html': htmlcode,
                        'item': item,
                        'loaded': false,
                        'callback': cb
                    });
                } else {
                    this.lazyQueue[top] = [{
                        'elem': elem,
                        'loaded': false,
                        'callback': cb
                    }];
                }
            }

        },
        'showImg': function(elem) {
            var imgs = elem.find('img');
            for (var i = 0; i < imgs.length; i++) {
                $(imgs[i]).attr('src', $(imgs[i]).attr('data-src')).fadeIn(500);
            }
        },
 $(window).bind("scroll", function() {
        var Ad = LETV.using('Plugin').AD;
        if (Ad.lazySt)
            clearTimeout(Ad.lazySt);
        Ad.lazySt = setTimeout(function() {
            var curHeight = $(window).height() + $(window).scrollTop(),
                lazyQueue = Ad.lazyQueue;
            for (var key in lazyQueue) {
                if (lazyQueue.hasOwnProperty(key)) {
                    var item = lazyQueue[key];
                    if (!item) continue;
                    for (var j = 0; j < item.length; j++) {
                        var mItem = item[j];
                        if (mItem && !mItem.loaded && Ad.isVisiable(mItem.elem)) {
                            if (mItem.loaded === false) {
                                mItem.cb && typeof mItem.cb == 'function' && mItem.cb();
                                Ad.showImg(mItem.elem);
                                mItem.loaded = true;
                            } else {
                                mItem = null;
                            }
                        }
                    }
                }
            }

        }, 200);
    });

  

posted @ 2016-09-12 13:31  飘然离去  阅读(344)  评论(0编辑  收藏  举报