如何在 Swiper 幻灯片中预加载视频

Jym*_*mbo 3 angular swiper.js

我正在使用 swiper(用于角度)将视频集成到我的幻灯片中(每张幻灯片一个视频)。当滑动幻灯片时,它非常滞后。然而,当视频已经加载时,例如当滑回到之前的视频时,滑动是平滑的。有人知道如何防止滞后行为吗?是否可以预加载视频?

Ezr*_*ton 6

您尝试过这个解决方案吗?

使用- :https lazyload.js: //github.com/verlok/lazyload

延迟加载视频: https://github.com/verlok/lazyload#lazy-video

由于 LazyLoad 不依赖于 jQuery,因此您可以在使用Angular、React 或 Vue.js 的 Web 应用程序中使用它,而无需包含 jQuery。阅读更多

对我来说效果很好。HTML 示例:

/* SWIPER */
var swiper = new Swiper(".swiper-container", {
  loop: true,
  pagination: {
    el: ".swiper-pagination",
    clickable: true
  }
});

/* LazyLoad */
var lazyLoadInstance = new LazyLoad({
    elements_selector: ".lazy",
    // ... more custom settings?
});

if (lazyLoadInstance) {
    console.log("our lazy content is ready");
    lazyLoadInstance.update();
}

/* swiper events */
swiper.on("slideChange", function() {
    // ... do something
});
Run Code Online (Sandbox Code Playgroud)
/* swiper */
html,
body {
  position: relative;
  height: 100%;
}
body {
  background: #eee;
  font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
  font-size: 14px;
  color: #000;
  margin: 0;
  padding: 0;
}
.swiper-container {
  width: 100%;
  height: 100%;
}
.swiper-slide {
  text-align: center;
  font-size: 18px;
  position: relative;
  background: #fff;
  /* Center slide text vertically */
  display: -webkit-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
}

video{
  height: 100%;
  width: auto;
}
Run Code Online (Sandbox Code Playgroud)
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.1/css/swiper.min.css" rel="stylesheet"/>
<div class="swiper-container">
  <div class="swiper-wrapper">
    <div class="swiper-slide">
      <video class="lazy" controls width="620" data-src="https://ak4.picdn.net/shutterstock/videos/1025732684/preview/stock-footage-two-male-colleagues-employees-cooperating-in-office-talking-working-together-at-workplace-smiling.mp4">
    <source type="video/mp4" data-src="https://ak4.picdn.net/shutterstock/videos/1025732684/preview/stock-footage-two-male-colleagues-employees-cooperating-in-office-talking-working-together-at-workplace-smiling.mp4">
</video>
    </div>
    <div class="swiper-slide">
      <video id="lazyTest" class="lazy" controls width="620" data-src="https://ia800300.us.archive.org/17/items/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4">
    <source type="video/mp4" data-src="https://ia800300.us.archive.org/17/items/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4">
</video>
    </div>
    <div class="swiper-slide">
      <video class="lazy" controls width="620" data-src="https://ak0.picdn.net/shutterstock/videos/1027882880/preview/stock-footage-young-entrepreneur-walkling-toward-city-sky-scrapers-dubai-urban-panorama-futuristic-digital-nomad.webm">
    <source type="video/mp4" data-src="https://ak0.picdn.net/shutterstock/videos/1027882880/preview/stock-footage-young-entrepreneur-walkling-toward-city-sky-scrapers-dubai-urban-panorama-futuristic-digital-nomad.webm">
</video>

    </div>

    <div class="swiper-slide">
      <video class="lazy" controls width="620" data-src="https://ak1.picdn.net/shutterstock/videos/25348301/preview/stock-footage--business-new-customers-sale-and-people-concept-thousands-of-people-formed-qr-code-crowd-flight.mp4">
    <source type="video/mp4" data-src="https://ak1.picdn.net/shutterstock/videos/25348301/preview/stock-footage--business-new-customers-sale-and-people-concept-thousands-of-people-formed-qr-code-crowd-flight.mp4">
</video>
    </div>

    <div class="swiper-slide">
      <video class="lazy" controls width="620" data-src="https://ak0.picdn.net/shutterstock/videos/18238960/preview/stock-footage-young-girl-on-the-hover-board-on-the-river-water-jet-spray-young-girl-on-the-flying-board-flies.mp4">
    <source type="video/mp4" data-src="https://ak0.picdn.net/shutterstock/videos/18238960/preview/stock-footage-young-girl-on-the-hover-board-on-the-river-water-jet-spray-young-girl-on-the-flying-board-flies.mp4">
</video>
    </div>
    <div class="swiper-slide">
      <video class="lazy" controls width="620" data-src="https://ak9.picdn.net/shutterstock/videos/1018564099/preview/stock-footage-two-friends-on-summer-vacation-or-holiday-run-on-wooden-boardwalk-on-alpine-mountain-lake-jump.mp4">
    <source type="video/mp4" data-src="https://ak9.picdn.net/shutterstock/videos/1018564099/preview/stock-footage-two-friends-on-summer-vacation-or-holiday-run-on-wooden-boardwalk-on-alpine-mountain-lake-jump.mp4">
</video>
    </div>
  </div>
  <!-- Add Pagination -->
  <div class="swiper-pagination"></div>
</div>


<!-- assets -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.1/js/swiper.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@12.0.0/dist/lazyload.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

代码笔: https: //codepen.io/ezra_siton/pen/wvwReJr

速度测试 (gtmetrix)

**相同的视频

懒惰的:

在此输入图像描述

没有懒惰:

在此输入图像描述