将图像加载到bootstrap carousel onClick

时间:2022-04-07 07:47:09


I'm working on an html site with a lot of images, to reduce loading time I'm going to load hidden images when user click on the cover image with javascript, then insert it into bootstrap carousel.

我正在开发一个包含大量图像的html网站,以减少加载时间当用户使用javascript点击封面图片时,我将加载隐藏的图像,然后将其插入bootstrap轮播。

I tried to make it, but unsuccessful. Images aren't load. Any help?
Here is my last code:

我试图成功,但不成功。图像无法加载。有帮助吗?这是我的最后一个代码:

HTML

<li>
  <a href="#" onclick="imgFilter('.ar-img')">
    <img src="images/AR/01.jpg" alt="">
  </a>
  <div class="accordion carousel-inner">
    <div class="item active">
      <img src="images/AR/01.jpg" alt="slide">
    </div>
    <div class="item">
      <img class="ar-img" src="" data-src="images/AR/02.jpg" alt="slide">
    </div>
    <div class="item">
      <img class="ar-img" src="" data-src="images/AR/03.jpg" alt="slide">
    </div>
  </div>
</li>

<li>
  <a href="#" onclick="imgFilter('.br-img')">
    <img src="images/BR/01.jpg" alt="">
  </a>
  <div class="accordion carousel-inner">
    <div class="item active">
      <img src="images/BR/01.jpg" alt="slide">
    </div>
    <div class="item">
      <img class="br-img" src="" data-src="images/BR/02.jpg" alt="slide">
    </div>
    <div class="item">
      <img class="br-img" src="" data-src="images/BR/03.jpg" alt="slide">
    </div>
  </div>
</li>

JS

function imgFilter(id) {
    var imgId = $(id);

    $("imgId").each(function(i) {
        $("this").setAttribute('src', $("this").getAttribute('data-src'));
    });
};

1 个解决方案

#1


0  

Something in the lines of:

有点像:

$(".item img").each(function(i) {
    $("this").setAttribute('src', $("this").getAttribute('data-src'));
}

however i would use jQuery plugin lazyload instead (http://www.appelsiini.net/projects/lazyload).

但是我会使用jQuery插件lazyload(http://www.appelsiini.net/projects/lazyload)。


Assumed you have css like:

假设你有css喜欢:

.item{
  width:200px;
  height:auto;
}
.item img{
  width:100%;
  height:auto;
}

#1


0  

Something in the lines of:

有点像:

$(".item img").each(function(i) {
    $("this").setAttribute('src', $("this").getAttribute('data-src'));
}

however i would use jQuery plugin lazyload instead (http://www.appelsiini.net/projects/lazyload).

但是我会使用jQuery插件lazyload(http://www.appelsiini.net/projects/lazyload)。


Assumed you have css like:

假设你有css喜欢:

.item{
  width:200px;
  height:auto;
}
.item img{
  width:100%;
  height:auto;
}