Skip to content

Instantly share code, notes, and snippets.

@yangjunjun
Created April 4, 2014 07:07
Show Gist options
  • Save yangjunjun/9969574 to your computer and use it in GitHub Desktop.
Save yangjunjun/9969574 to your computer and use it in GitHub Desktop.
Images lazy load
;(function($, window, document){
$.yangLazyLoad = function(){
var img = $('img[data-src]');
img.each(function(){
this.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC';
})
$(document).scroll(function(){
img.each(function(){
var _this = this;
var top = $(_this).offset().top;
var scrollTop = $(document).scrollTop();
var wHeight = $(window).height();
if(wHeight + scrollTop >= top){
var url = $(_this).attr('data-src');
$(_this).attr('src', url);
}
})
})
}
}(jQuery, window, document))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment