Skip to content

Instantly share code, notes, and snippets.

@dylanvalade
Last active April 23, 2022 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylanvalade/8633913 to your computer and use it in GitHub Desktop.
Save dylanvalade/8633913 to your computer and use it in GitHub Desktop.
Ajax jQuery Lazy Image Load
/*
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"));
}
});
}
@fuad-salomon-tierconnect-com

It works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment