【发布时间】:2021-12-04 02:12:45
【问题描述】:
我有一个网页,其中包含来自不同来源的 30 个 iFrame 视频。
这导致加载时间非常长,所以我想创建一种延迟加载。
因此,在页面加载时,它会显示带有播放按钮叠加层的视频图像。 使用 JavaScript/Jquery OnClick,它将添加 iFrame,并隐藏图像。
问题
我不知道如何循环这个函数,这样我就可以避免复制 Javascript 30 多次。
JS 小提琴 https://jsfiddle.net/wmLdabon/
HTML
<div class="embed-responsive embed-responsive-16by9" id="iframeHolder1">
<div class="playButtonContainer" style="height:300px;width:100%;padding-top: 24%;background-image:url(https://i.insider.com/5c79a8cfeb3ce837863155f5?width=700&format=jpeg&auto=webp);background-repeat: no-repeat;">
<div class="playButton" id="playButton1">Play Video</div>
</div>
</div>
<div class="embed-responsive embed-responsive-16by9" id="iframeHolder2">
<div class="playButtonContainer" style="height:300px;width:100%;padding-top: 24%;background-image:url(https://i.insider.com/5ea6fd9dd553f808ba5bf897?width=700&format=jpeg&auto=webp);background-repeat: no-repeat;">
<div class="playButton" id="playButton2">Play Video</div>
</div>
</div>
JAVASCRIPT
//First video
$(function(){
$('#playButton1').click(function(){
if(!$('#iframe').length) {
$('#iframeHolder1').html('<iframe class="embed-responsive-item" style="height:300px; width:100%" src="https://www.youtube.com/embed/kujV1qHr1ow" allowfullscreen></iframe>');
}
});
});
//Repeat for 2nd video.
$(function(){
$('#playButton2').click(function(){
if(!$('#iframe').length) {
$('#iframeHolder2').html('<iframe class="embed-responsive-item" style="height:300px; width:100%" src="https://www.youtube.com/embed/WDlu1OhvYBM" allowfullscreen></iframe>');
}
});
});
关于如何循环播放有什么建议吗?
谢谢!
【问题讨论】:
标签: javascript html jquery css iframe