jquery - 猫头鹰懒惰类中的 Gif 加载图像 Owl Carousel LazyLoad?

标签 jquery html css owl-carousel

我想在 owl carousel2 中设置预加载器图像。我已经阅读了文档,但我没有找到任何在 lazyload 插件选项中使用预加载器图像的信息。

这是插件网站:

https://owlcarousel2.github.io/OwlCarousel2/

我的代码:


<div class="single-product owl-carousel owl-theme">
  <a href="https://source.unsplash.com/0utRJ1mZoZg/1080x1350" data-fancybox="images">
    <img data-src="https://source.unsplash.com/0utRJ1mZoZg/600x600" class="img-fluid owl-lazy" />
  </a>

  <a href="https://source.unsplash.com/aiNU4cA4UzQ/1080x1350" data-fancybox="images">
    <img data-src="https://source.unsplash.com/aiNU4cA4UzQ/600x600" class="img-fluid owl-lazy" />
  </a>

  <a href="https://source.unsplash.com/295NLwGdrKM/1080x1350" data-fancybox="images">
    <img data-src="https://source.unsplash.com/295NLwGdrKM/600x600" class="img-fluid owl-lazy" />
  </a>

  <a href="https://source.unsplash.com/NzsTFFB6Ng8/1080x1350" data-fancybox="images">
    <img data-src="https://source.unsplash.com/NzsTFFB6Ng8/600x600" class="img-fluid owl-lazy" />
  </a>
</div>
$('.single-product').owlCarousel({
 loop : true,
 items : 1,
 nav : false,
 lazyLoad: true,
});

我需要提供预加载器 gif 图像,以获得更好的用户体验。

你能帮帮我吗? 非常感谢

最佳答案

您可以设置加载程序 gif 的背景图像。此外,我将 OwlCarousel 调用包装在 $(window).load() 中,这样它会在启动之前等待图像加载,同时加载程序会处理任何 Flash Of Unstyled内容(FOUC)

$(window).on('load', function() {
  $('.single-product').owlCarousel({
    autoHeight: true,
    lazyLoad: true,
    items: 1,
    autoplay: true,
    nav: false,
    dots: true,
    loop: true,
  });
});
.owl-carousel {
  height: 300px;
}

.owl-carousel {
  background: url("https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/ajax-loader.gif") no-repeat center center;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.min.css">

<div class="single-product owl-carousel owl-theme">
  <a href="https://source.unsplash.com/0utRJ1mZoZg/1080x1350" data-fancybox="images">
    <img data-src="https://source.unsplash.com/0utRJ1mZoZg/600x600" class="img-fluid owl-lazy" />
  </a>

  <a href="https://source.unsplash.com/aiNU4cA4UzQ/1080x1350" data-fancybox="images">
    <img data-src="https://source.unsplash.com/aiNU4cA4UzQ/600x600" class="img-fluid owl-lazy" />
  </a>

  <a href="https://source.unsplash.com/295NLwGdrKM/1080x1350" data-fancybox="images">
    <img data-src="https://source.unsplash.com/295NLwGdrKM/600x600" class="img-fluid owl-lazy" />
  </a>

  <a href="https://source.unsplash.com/NzsTFFB6Ng8/1080x1350" data-fancybox="images">
    <img data-src="https://source.unsplash.com/NzsTFFB6Ng8/600x600" class="img-fluid owl-lazy" />
  </a>
</div>

<script src="https://code.jquery.com/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js"></script>

关于jquery - 猫头鹰懒惰类中的 Gif 加载图像 Owl Carousel LazyLoad?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56590816/

相关文章:

html - 使用 CSS3 进行透视裁剪

html - anchor 标签不起作用并保持选中状态,直到我再次单击

javascript - 使用虚拟键盘 JavaScript 获取文本字段的 ID

html - 选中时如何在复选框周围应用边框?

javascript - 克隆一个 <tr> 并将 <td> 中元素的名称属性增加 1

php - 如何创建自定义 jquery 轮播?

html - 每当关注 `tr` 时更改 `input` 样式?

javascript - 使菜单上下滑动

javascript - 如何在ul中选择下一个匹配的子项并触发点击事件

javascript - jQuery 禁用点击但不禁用链接