I use lazyload() on my ecommerce website. lazyload() works great. I use this code to do that:
$(function(){
$(\"img.lazy\").lazyload({
effect : \"fadeIn
For my scenario when the page is loaded I will get list of template with template Id. Based on that template Id I use jQuery Lazy to load base64 encoded images using ajax and load image lazily on my page.
Hope you will get some idea of my approach
$(document).ready(function() {
let imageArray = [0, 1, 10, 100, 1000, 1001];
let parsedImageArray = $.map(imageArray, function(value) {
return "<div class='lazy' data-loader='ajaxLoader' data-src='/echo/html/' data-method='post' contentId='" + value + "'></div><br>";
});
$("#content").html(parsedImageArray);
console.log("Here" + parsedImageArray)
});
$(document).bind('DOMNodeInserted', function(e) {
$('.lazy').lazy({
effect: "fadeIn",
event: "scrollstop",
skip_invisible: true,
removeAttribute: false,
ajaxLoader: function(element, response) {
let thumbnailId = element.attr("contentId");
console.log("Here" + thumbnaiId);
$.get("https://picsum.photos/id/" + thumbnailId, function(srcValue, status) {
//for me i have to pull the base64 encode image from my server
let defaultImageDom = generateThumbnailImage(tempSrc);
element.html(defaultImageDom);
element.removeClass('lazy');
}).fail(function(jqXHR, textStatus, errorThrown) {
let defaultImageDom = generateThumbnailImage("");
element.html(defaultImageDom);
element.append("<em>Error Getting Thumbnail Preview</em>");
element.removeClass('lazy');
});
},
});
});
function generateThumbnailImage(srcValue) {
let defaultImageDom = new Image();
defaultImageDom.height = 200;
defaultImageDom.style.borderColor = "#d5d5d5";
defaultImageDom.style.borderWidth = "1px";
defaultImageDom.style.borderStyle = "solid";
if (srcValue && srcValue.length > 0) {
defaultImageDom.src = srcValue;
} else {
//set your fallback image link
defaultImageDom.src = "data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=";
}
return defaultImageDom;
}
.lazy {
width: 700px;
height: 467px;
display: block;
/* optional way, set loading as background */
background-image: 'data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=';
background-repeat: no-repeat;
background-position: 50% 50%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.lazy/1.7.9/jquery.lazy.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.lazy/1.7.9/jquery.lazy.plugins.min.js"></script>
<div id="content">
</div>
Shortly, the best optimized way to do this is to call the lazyload in your ajax's success function:
$.ajax({
url: "your url here",
type: "Post",
success: function(response){
// handle your success callback here
$("img.lazy").lazyload({
effect : "fadeIn"
});
}
});
But if you want to centralize your call to the lazyload plugin, you have 2 options:
First: (not recommended due to performance hit)
$(document).on('DOMNodeInserted', '#container', function() {
$("img.lazy").lazyload({
effect: 'fadeIn'
});
});
but note here the "#container", it is the container's selector where you will show your new loaded stuff, so the code above will listen inside this container for any newly added stuff to run the lazyload again on new images,
Second: (recommended)
Calling your custom lazyload by adding it to your JQuery:
$.fn.myLazyLoad = function() {
this.lazyload({
effect : "fadeIn"
});
};
then, call it in all your AJAX requests:
$.ajax({
url: "your url here",
type: "Post",
success: function(response){
// handle your success callback here
$("img.lazy").myLazyLoad();
}
});