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

时间:2019-09-06 08:55:13

标签: angular swiper

我正在使用滑动器(角度)将视频集成到幻灯片中(每个幻灯片一个视频)。滑动幻灯片时,它会很滞后。但是,当视频已经加载时,例如滑动之前的视频时,滑动很流畅。有人知道如何防止迟钝的行为吗?是否可以预加载视频?

1 个答案:

答案 0 :(得分:1)

您尝试过此解决方案吗?

使用-lazyload.jshttps://github.com/verlok/lazyload

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

  

由于LazyLoad不依赖jQuery,因此可以在Web应用程序中使用它   使用 Angular ,React或Vue.js,而无需包含jQuery。 Read More

对我来说很好。 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
});
/* 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;
}
<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>

Codepen: https://codepen.io/ezra_siton/pen/wvwReJr

速度测试(gtmetrix)

**相同​​的视频

懒惰:

enter image description here

没有偷懒:

enter image description here