Jquery lazyload with ajax

后端 未结 8 1476
-上瘾入骨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:07

    I use the code below and it worked:

    $(document).on('ajaxStop', function() {
       $("img.lazy").lazyload({
            effect: 'fadeIn'
        });
    });
    
    0 讨论(0)
  • 2021-01-18 00:08
       $(document).ajaxStop(function() {$('.loading').removeClass('in');
        $("img.lazy").lazyload({
                    effect: "fadeIn",
                   container: $("#scrollable"),
                   threshold: 100,
                })
    
          });
    

    works for ajax images. but however, not showing the first image by default. the update is not triggered.

    0 讨论(0)
  • 2021-01-18 00:09

    NOTE: At the time this answer was accepted, this worked. The lazy loading plugin changed and now it is broken. I cannot unaccept the answer nor can I delete it.

    There is another Q&A that suggests handling it as part of the AJAX request responsible for inserting the additional DOM:

    Binding image lazy loading to new images inserted after ajax request

    However, this is how I would do it:

    $(document).on('DOMNodeInserted', 'img.lazy', function() {
        $(this).lazyload({
            effect: 'fadeIn'
        });
    });
    

    Demo: jsfiddle

    0 讨论(0)
  • 2021-01-18 00:11

    Lazyload for images loaded after window load (e.g. AJAX) will not display any images before a scroll event. This is because it is internally hardcoded to trigger the initial update after the window load event. All the above answers, for now, are wrong.

    0 讨论(0)
  • 2021-01-18 00:21

    In addition to AbdelHady's answer you can lazy load new images on success as a callback or use when() this way:

       $.when(load_new_image()).done(function(i){
    
                if($('img.lazy').length){
    
                     $('img.lazy').lazyload({ 
    
                              effect:'fadeIn'
    
                     }).removeClass('lazy').addClass('lazyloaded');
    
                }
    
            });
    
            function load_new_image() {
    
                return $.ajax({
                    url: "your url here",
                    type: "Post",
                    success: function(response){
    
                         //append new content
                        $('#container').append(response);
    
                    }
                });
            }
    

    NOTA BENE

    0 讨论(0)
  • 2021-01-18 00:25

    I have same problem, when using waypoints infinite scroll with lazy load image. (but not work on ajax content).

    and I fixed that issue with this :

    $("img.lazy").lazyload({
        effect : "fadeIn",
        event: "scrollstop",
        skip_invisible : true
    }).removeClass('lazy');
    
    $(document).bind('DOMNodeInserted', function(e) {
        $("img.lazy").lazyload({
            effect : "fadeIn",
            event: "scrollstop",
            skip_invisible : true
        }).removeClass('lazy');
    });
    

    I bind the lazy load configuration on DOMNodeInserted event.

    0 讨论(0)
提交回复
热议问题