在水平滑块中延迟加载图像

时间:2017-08-10 22:27:57

标签: jquery lazy-loading jquery-lazyload

我使用jquery lazyload和一个阈值来缓慢加载此站点上的图像:https://bm-translations.de/#leistungen

//Lazyload Images with Threshold https://www.appelsiini.net/projects/lazyload
$(function() {
    $("img.lazy").lazyload({
        threshold : 400
    });
}); 

所以图片srcdata-original取代。当我滚动到元素时,它应该将其更改为src,因为它。

但是出于某种原因,它加载了引导滑块中的图像太慢(当我点击它或者它自动滑动时某些图像没有加载),正如你所看到的那样。如何调整此代码,以便它也适用于此?

滑块的结构如下:https://www.w3schools.com/bootstrap/bootstrap_carousel.asp

Carousel JS是:

$('#myCarousel').carousel({
        interval: 9000,
    });

    // handles the carousel buttons
    $('[id^=carousel-selector-]').click( function(){
      var id_selector = $(this).attr("id");
      var id = id_selector.substr(id_selector.length -1);
      id = parseInt(id);
      $('#myCarousel').carousel(id);
      $('[id^=carousel-selector-]').removeClass('selected');
      $(this).addClass('selected');
    });

    // when the carousel slides, auto update
    $('#myCarousel').on('slide.bs.carousel', function (e) {
      var id = $('.item.active').data('slide-number');
      id = parseInt(id)+1;
      $('[id^=carousel-selector-]').removeClass('selected');
      $('[id=carousel-selector-'+id+']').addClass('selected');
    });

我尝试了这个,但后来又不再滑动了:

$('#myCarousel').carousel({
        interval: 9000,
        scroll: {
            onBefore: function( data ) {
              var $current = data.items.visible.first(),
                  visible = data.items.visible,
                  src = visible.data('src');

              visible.attr('src', src);
            }
        }
    });

如何修复它,所以它在点击/自动滑动之前是懒惰加载还是至少延迟加载整个轮播图像的阈值?

1 个答案:

答案 0 :(得分:3)

我建议您使用" owl.carousel.js"插入。它更好,功能更多。



$('.owl-carousel').owlCarousel({
    items:1,
    lazyLoad:true,
    loop:true,
    margin:10,
    autoplay:true,
    autoplayTimeout:3000,
    autoplayHoverPause:true,
});

<link href="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/assets/owl.carousel.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://owlcarousel2.github.io/OwlCarousel2/assets/owlcarousel/owl.carousel.js"></script>
<div class="owl-carousel owl-theme">
    <img class="owl-lazy" data-src="https://placehold.it/350x250&text=1" data-src-retina="https://placehold.it/350x250&text=1-retina" alt="">
    <img class="owl-lazy" data-src="https://placehold.it/350x250&text=2" data-src-retina="https://placehold.it/350x250&text=2-retina" alt="">
    <img class="owl-lazy" data-src="https://placehold.it/350x250&text=3" alt="">
    <img class="owl-lazy" data-src="https://placehold.it/350x250&text=4" alt="">
    <img class="owl-lazy" data-src="https://placehold.it/350x250&text=5" alt="">
    <img class="owl-lazy" data-src="https://placehold.it/350x250&text=6" alt="">
    <img class="owl-lazy" data-src="https://placehold.it/350x250&text=7" alt="">
    <img class="owl-lazy" data-src="https://placehold.it/350x250&text=8" alt="">
    <img class="owl-lazy" data-src="https://placehold.it/350x250&text=9" alt="">
    <img class="owl-lazy" data-src="https://placehold.it/350x250&text=10" alt="">
    <img class="owl-lazy" data-src="https://placehold.it/350x250&text=11" alt="">
</div>
&#13;
&#13;
&#13;