Commit 7797d073 authored by 杨培's avatar 杨培

首页图片懒加载

parent 7190b4e2
$.fn.lazyload = function(container, fn) {
if (typeof container == "function") {
fn = container;
container = null;
}
if (!container) container = window;
container = $(container);
if (!fn) fn = $.noop;
var cache = [];
//Cached
$(this).each(function() {
var tag = this.nodeName.toLowerCase(), url = $(this).data("src");
if (url) cache.push({obj:this, tag:this.nodeName.toLowerCase(), url:url});
if (tag == "img" && !$(this).attr("src")) $(this).attr("src", "data:image/gif;base64,R0lGODlhAQABAIAAAOjo6AAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==");
});
function check() {
var contHeight = container.height();
var contop = container.get(0) === window ? container.scrollTop() : container.offset().top;
$.each(cache, function(i, data) {
if (!data.obj) return;
var o = $(data.obj), url = data.url;
var post = o.offset().top - contop;
var posb = post + o.height();
if (o.is(':visible') && (post >= 0 && post < contHeight) || (posb > 0 && posb <= contHeight)) {
if (data.tag === "img") {
fn.call(o.attr("src", url)[0]);
} else {
o.load(url, {}, function() {
fn.call(o[0]);
});
}
data.obj = null;
}
});
}
check();
container.on("scroll", check);
return this;
};
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment