lazyload
by this_mong
HTML
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lazyload.js"></script>
<div class="lazyload">
<img data-original="https://via.placeholder.com/150x150.png" width="150" height="150">
</div>
<div class="lazyload">
<img data-original="https://via.placeholder.com/150x150.png" width="150" height="150">
</div>
<div class="lazyload">
<img data-original="https://via.placeholder.com/150x150.png" width="150" height="150">
</div>
<div class="lazyload">
<img data-original="https://via.placeholder.com/150x150.png" width="150" height="150">
</div>
CSS
div{margin-bottom:400px;display:block;}
JavaScript
/*
* Lazy Load - jQuery plugin for lazy loading images
*
* Copyright (c) 2007-2012 Mika Tuupola
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* http://www.appelsiini.net/projects/lazyload
*
* Version: 1.8.2
*
*/
(function($, window, document, undefined) {
var $window = $(window);
$.fn.lazyload = function(options) {
var elements = this;
var $container;
var settings = {
threshold: 0,
failure_limit: 0,
event: "scroll",
effect: "show",
container: window,
data_attribute: "original",
skip_invisible: true,
appear: null,
load: null
};
function update() {
var counter = 0;
elements.each(function() {
var $this = $(this);
if (settings.skip_invisible && !$this.is(":visible")) {
return;
}
if ($.abovethetop(this, settings) || $.leftofbegin(this, settings)) { /* Nothing. */
} else if (!$.belowthefold(this, settings) && !$.rightoffold(this, settings)) {
$this.trigger("appear"); /* if we found an image we'll load, reset the counter */
counter = 0;
} else {
if (++counter > settings.failure_limit) {
return false;
}
}
});
}
if (options) { /* Maintain BC for a couple of versions. */
if (undefined !== options.failurelimit) {
options.failure_limit = options.failurelimit;
delete options.failurelimit;
}
if (undefined !== options.effectspeed) {
options.effect_speed = options.effectspeed;
delete options.effectspeed;
}
$.extend(settings, options);
...