Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mpjura
Created December 30, 2011 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpjura/1540647 to your computer and use it in GitHub Desktop.
Save mpjura/1540647 to your computer and use it in GitHub Desktop.
Lazy Loading Images w/ jQuery
<div class="lazyImage" data-src="http://baconmockup.com/200/200" data-width="200" data-height="200" data-title="Mmmm Bacon"></div>
$(function() {
var b, e;
b = $(window);
e = $('.lazyImage');
b.scroll(function() {
$.each(e, function() {
var c = $(this), a, d;
a = c.offset();
d = c.data();
if(!d.loaded && a.top <= b.height() + b.scrollTop()) {
a = new Image, $.extend(a, d), a.onload = function() {
c.prepend(this);
$(this).fadeIn();
c.removeClass('lazyImage');
e = $('.lazyImage')
}, c.data('loaded', !0)
}
})
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment