【问题标题】:Bootstrap Carousel Lazy Load引导轮播延迟加载
【发布时间】:2012-10-02 19:48:17
【问题描述】:

我正在尝试让引导轮播延迟加载。看起来这应该是直截了当的,但我什至没有在这里遇到任何错误来进行故障排除。 JS:

<script type="text/javascript">
    $('#bigCarousel').on("slid", function() {
        // load the next image after the current one slid
        var $nextImage = $('.active.item', this).next('.item').find('img');
        $nextImage.attr('src', $nextImage.data('lazy-load-src'));
    });

</script>

然后是html:

<div id="bigCarousel" class="carousel slide">
  <div class="carousel-inner">
    <div class="active item">
        <img src="assets/bigimage1.jpg" class="img-big">
    </div>
    <div class="item">
        <img data-lazy-load-src="assets/menu-header2.jpg" class="img-big">
    </div>
 </div>
</div>

firebug 中没有 JS 错误或我能找出的任何其他内容,但我的图像没有加载。可能这里有些愚蠢的简单......

【问题讨论】:

标签: twitter-bootstrap twitter-bootstrap-2


【解决方案1】:

最简单的解决方案是:

<div id="carousel" class="carousel slide lazy-load" data-ride="carousel" data-interval="false">
<div class="carousel-inner">
  <div class="item active"><img src="image1.jpg"></div>
  <div class="item"><img data-src="image2.jpg"></div>
  <div class="item"><img data-src="image3.jpg"></div>
</div>
<!-- controls -->
<ol class="carousel-indicators">
    <li data-target="#carousel" data-slide-to="0" class="active"></li>
    <li data-target="#carousel" data-slide-to="1"></li>
    <li data-target="#carousel" data-slide-to="2"></li>
  </ol>
</div>

<script>
$(function() {
    $('.carousel.lazy-load').bind('slide.bs.carousel', function (e) {
        var image = $(e.relatedTarget).find('img[data-src]');
        image.attr('src', image.data('src'));
        image.removeAttr('data-src');
    });
});
</script>

就是这样!

【讨论】:

    【解决方案2】:

    我认为问题在于你的 nextImage 变量中的 $。除非您尝试提前预加载 1 张幻灯片,否则我也会切换到“幻灯片”事件而不是“幻灯片”事件。此外,您可能应该考虑向前和向后滚动。这包括检查从第一张照片到最后一张照片的循环,反之亦然。

    $('#myCarousel').on("slide", function(e) {
    
            //SCROLLING LEFT
            var prevItem = $('.active.item', this).prev('.item');
    
            //Account for looping to LAST image
            if(!prevItem.length){
                prevItem = $('.active.item', this).siblings(":last");
            }
    
            //Get image selector
            prevImage = prevItem.find('img');
    
            //Remove class to not load again - probably unnecessary
            if(prevImage.hasClass('lazy-load')){
                prevImage.removeClass('lazy-load');
                prevImage.attr('src', prevImage.data('lazy-load-src'));
            }
    
            //SCROLLING RIGHT
            var nextItem = $('.active.item', this).next('.item');
    
            //Account for looping to FIRST image
            if(!nextItem.length){
                nextItem = $('.active.item', this).siblings(":first");
            }
    
            //Get image selector
            nextImage = nextItem.find('img');
    
            //Remove class to not load again - probably unnecessary
            if(nextImage.hasClass('lazy-load')){
                nextImage.removeClass('lazy-load');
                nextImage.attr('src', nextImage.data('lazy-load-src'));
            }
    
        });
    

    【讨论】:

    【解决方案3】:

    我有一个解决办法,你只需要在临时图片属性中设置img-url/path, 见下面的代码 img 有属性 url

    <div class="item">
     <img url="./image/1.jpg" src="">
      <div class="carousel-caption">
       <h4>title</h4>
       <p>content</p>
      </div>
    </div>
    

    当事件slid时,设置值:src = url

    $('document').ready(function() {     
            $('#myCarousel').on('slid', function (e) {
                var url = $('.item.active').find("img").attr('url'); 
                $('.item.active').find("img").attr("src", url); //set value : src = url
            });
    });
    

    【讨论】:

      【解决方案4】:
      $('#myCarousel').on('slid', function() {
          var $nextImage = $('.active.item', this).next('.item').find('img');
          $nextImage.attr('src', $nextImage.data('data-lazy-load-src'));
      });
      

      您正在加载为$nextImage.data('lazy-load-src'),但根据脚本应该是$nextImage.data('data-lazy-load-src')

      【讨论】:

      • 对我来说,它不适用于“滑动”事件。如果与 'slide.bs.carousel' 一起使用就可以了。谢谢@Sameera Thilakasiri
      猜你喜欢
      • 1970-01-01
      • 2015-02-24
      • 2016-07-05
      • 2017-11-26
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多