lazy load not work in bootstrap carousel

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

I create carousel using bootstrap 3 like this :

HTML :

相关标签:
4条回答
  • 2020-12-06 05:58

    http://jsfiddle.net/ys4je40u/

    1. add lazy-load class to item object
    2. use slide.bs.carousel event

    CSS:

    .lazy-load {
        background: url("http://www.mozart.it/images/preloader.gif") center center no-repeat;
    }
    

    JQ:

    var cHeight = 0;
    
    $('#myCarousel').on('slide.bs.carousel', function (e) {
        var $nextImage = null;
    
        $activeItem = $('.active.item', this);
    
        if (e.direction == 'left'){
            $nextImage = $activeItem.next('.item').find('img');
        } else {
            if ($activeItem.index() == 0){
                $nextImage = $('img:last', $activeItem.parent());
            } else {
                $nextImage = $activeItem.prev('.item').find('img');
            }
        }
    
        // 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', '');
        }
    });
    
    0 讨论(0)
  • 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', '');
      }
    });
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    
    <div class="container-fluid">
    
      <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
        <!-- Indicators -->
        <ol class="carousel-indicators">
          <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
          <li data-target="#carousel-example-generic" data-slide-to="1"></li>
          <li data-target="#carousel-example-generic" data-slide-to="2"></li>
          <li data-target="#carousel-example-generic" data-slide-to="3"></li>
          <li data-target="#carousel-example-generic" data-slide-to="4"></li>
        </ol>
    
        <!-- Wrapper for slides -->
        <div class="carousel-inner" role="listbox">
          <div class="item active">
            <img src="http://placehold.it/5000x1000">
          </div>
          <div class="item">
            <img data-lazy-load-src="http://placehold.it/5001x1000">
          </div>
          <div class="item">
            <img data-lazy-load-src="http://placehold.it/5002x1000">
          </div>
          <div class="item">
            <img data-lazy-load-src="http://placehold.it/5003x1000">
          </div>
          <div class="item">
            <img data-lazy-load-src="http://placehold.it/5004x1000">
          </div>
        </div>
    
        <!-- Controls -->
        <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
          <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
          <span class="sr-only">Previous</span>
        </a>
        <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
          <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
          <span class="sr-only">Next</span>
        </a>
      </div>
    
    </div>

    0 讨论(0)
  • 2020-12-06 06:11

    If you have multiple images in your slide, you have to use this :

    var cHeight = 0;
    
    $('#carousel').on('slide.bs.carousel', function (e) {
        var $nextImage = null;
    
        $activeItem = $('.active.item', this);
    
        if (e.direction == 'left'){
            $nextImage = $activeItem.next('.item').find('img');
        } else {
            if ($activeItem.index() == 0){
                $nextImage = $('img:last', $activeItem.parent());
            } else {
                $nextImage = $activeItem.prev('.item').find('img');
            }
        }
    
        // prevents the slide decrease in height
        if (cHeight == 0) {
           cHeight = $(this).height();
           $activeItem.next('.item').height(cHeight);
        }
    
        // => Changes here ! 
        // prevents the loaded images if it is already loaded
        $nextImage.each(function(){
            var $this = $(this),
                src = $this.data('original');
    
            if (typeof src !== "undefined" && src != "") {
               $this.attr('src', src)
               $this.data('original', '');
            }
        });
    });
    
    0 讨论(0)
  • 2020-12-06 06:15

    Created this "kinda generic" solution some months ago and had no issues so far...

    var $ajaxCarousel = $('.carousel-ajax');
    
    if ($ajaxCarousel.length > 0) {
        $ajaxCarousel.on('slide.bs.carousel', function (e) {
            var $upcomingImage = $(e.relatedTarget).find('img');
            if (typeof $upcomingImage.attr('src') === 'undefined') {
                $upcomingImage.attr('src', $upcomingImage.data('src'));
            }
        });
        // preload first image
        $ajaxCarousel.each(function() {
            var $firstImage = $(this).find('[data-slide-number=0]').find('img');
    
            $firstImage.attr('src', $firstImage.data('src'));
        });
    }
    

    All you have to do in HTML is add "carousel-ajax" class to your existing carousel and prepend "data-" in front of your img's src tags:

    <img data-src="..." />
    
    0 讨论(0)
提交回复
热议问题