首页 > 代码库 > 页面的懒加载
页面的懒加载
‘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); });
页面的懒加载
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。