Jquery lazyload with ajax

后端 未结 8 1491
-上瘾入骨i
-上瘾入骨i 2021-01-17 23:26

I use lazyload() on my ecommerce website. lazyload() works great. I use this code to do that:

$(function(){
  $(\"img.lazy\").lazyload({ 
  effect : \"fadeIn         


        
8条回答
  •  滥情空心
    2021-01-18 00:27

    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 "

    "; }); $("#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("Error Getting Thumbnail Preview"); 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%;
    }
    
    
    
    
    

提交回复
热议问题