Last active
April 23, 2022 03:50
-
-
Save dylanvalade/8633913 to your computer and use it in GitHub Desktop.
Ajax jQuery Lazy Image Load
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
/* | |
For jQuery.lazy https://github.com/eisbehr-/jquery.lazy | |
You MUST have a src value on the img for lazy loading to run even if the preloader.gif doesn't exist | |
Do not leave that src attribute blank: | |
<img class="load" data-src="path_to/lazy-loaded-image.jpg" src="path_to/preloader.gif" /> | |
In my ajax callback: | |
loadImages('img.load'); | |
*/ | |
// Lazy load images | |
function loadImages(selector){ | |
$(selector).lazy({ | |
bind: 'event', | |
delay: 0, | |
effect: 'fadeIn', | |
effectTime: 2000, | |
enableThrottle: true, | |
throttle: 250, | |
afterLoad: function(element) { | |
$(element).removeClass('load'); | |
}, | |
beforeLoad: function(element) { | |
// Lazy will call this function, before the image gets loaded | |
console.log('Loading' + element.attr('data-src')); | |
}, | |
onLoad: function(element) { | |
// while loading the image, especially on big images, this function will be called | |
}, | |
onError: function(element) { | |
// if the image could not get loaded successfully, this is the triggered function | |
console.log("error loading image: " + element.attr("data-src")); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works!