gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:42:00 26 4
gpt4 key购买 nike

我正在使用滑动器(用于 Angular )将视频整合到我的幻灯片中(每张幻灯片一个视频)。在幻灯片中滑动时,它非常滞后。但是,当视频已经加载时,例如滑动回之前的视频时,滑动很流畅。有人知道如何防止滞后行为吗?是否可以预加载视频?

最佳答案

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

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

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

As LazyLoad doesn't rely on jQuery, you can use it in web applications using Angular, React or Vue.js without the need to include 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>

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

速度测试 (gtmetrix)

**相同的视频

懒惰:

enter image description here

没有懒惰:

enter image description here

关于angular - 如何在 Swiper 幻灯片中预加载视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57818807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com