Created
December 30, 2011 17:14
-
-
Save mpjura/1540647 to your computer and use it in GitHub Desktop.
Lazy Loading Images w/ jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="lazyImage" data-src="http://baconmockup.com/200/200" data-width="200" data-height="200" data-title="Mmmm Bacon"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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