码迷,mamicode.com
首页 > 其他好文 > 详细

页面的懒加载

时间:2016-09-12 14:27:58      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

        ‘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);
    });

  

页面的懒加载

标签:

原文地址:http://www.cnblogs.com/coding4/p/5864490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!