As shown below:
/** * picLazyLoad The picture is delayed to load, including the background image* $(img).picLazyLoad({...}) * data-original Preload image address* alon */;(function($){ $.fn.imgLazyLoad = function(settings){ var $this = $(this), _winScrollTop = 0, _winHeight = $(window).height(); settings = $.extend({ threshold: 0, // Placeholder loading in advance: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC', callback:function(){} }, settings||{}); // Execute lazy loading image lazyLoadPic(); // Scroll triggers the image change $(window).on('scroll',function(){ _winScrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop; lazyLoadPic(); }); // Lazy loading image function lazyLoadPic(){ $this.each(function(){ var $self = $(this); if($self.is('img')){ if($self.attr('data-original')){ var _offsetTop = $self.offset().top; if((_offsetTop - settings.threshold) <= (_winHeight + _winScrollTop)){ $self.attr('src',$self.attr('data-original')); $self.removeAttr('data-original'); $self.removeClass('loadH'); settings.callback($self); } } }else{ if($self.attr('data-original')){// Default placeholder image if($self.css('background-image') == 'none'){ $self.css('background-image','url('+settings.placeholder+')'); } var _offsetTop = $self.offset().top; if((_offsetTop - settings.threshold) <= (_winHeight + _winScrollTop)){ $self.css('background-image','url('+$self.attr('data-original')+')'); $self.removeAttr('data-original'); settings.callback($self); } } } } }); } } } })(Zepto);Call
$('img').imgLazyLoad({callback:function(data){ })The above article picLazyLoad to realize delayed image loading (including background pictures) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.