lazy load not work in bootstrap carousel

后端 未结 4 1733
渐次进展
渐次进展 2020-12-06 05:44

I create carousel using bootstrap 3 like this :

HTML :

4条回答
  •  心在旅途
    2020-12-06 06:05

    The answer by dm4web does not account for skipping slides by directly clicking the carousel indicators. This version correctly loads an image regardless of how you slide to it, using Bootstrap's event.relatedTarget (the item being slid into place, see the official documentation). Try running the snippet below and clicking on the indicators to switch several slides ahead.

    var cHeight = 0;
    
    $('#carousel-example-generic').on('slide.bs.carousel', function(e) {
    
      var $nextImage = $(e.relatedTarget).find('img');
    
      $activeItem = $('.active.item', this);
    
      // prevents the slide decrease in height
      if (cHeight == 0) {
        cHeight = $(this).height();
        $activeItem.next('.item').height(cHeight);
      }
    
      // prevents the loaded image if it is already loaded
      var src = $nextImage.data('lazy-load-src');
    
      if (typeof src !== "undefined" && src != "") {
        $nextImage.attr('src', src)
        $nextImage.data('lazy-load-src', '');
      }
    });
    
    
    
    
    

提交回复
热议问题