javascript - 如何防止两次加载 Vimeo 视频播放器 iFrame
问题描述
我在 flexbox 容器中显示 Vimeo 视频 iFrame。我正在用 JS 设置 iFrame 的大小。问题是页面加载了两次,首先我猜是标准设置,然后是我用 JS 设置的那些。
如何防止双重加载?
我给你一个 JSFiddle,因为由于 cookie 沙盒,在 StackSnippet 中看不到效果
HTML
<div class="dc-video-row">
<div class="dc-video-content">
<iframe
src='https://player.vimeo.com/video/436703027'
frameborder='0'
webkitAllowFullScreen mozallowfullscreen allowFullScreen>
</iframe>
</div>
<div class="dc-text-content">
text
</div>
</div>
<script src="https://player.vimeo.com/api/player.js"></script>
CSS
.dc-video-row {
display: flex;
flex-direction: row;
gap: 1rem;
}
.dc-text-content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background: yellow;
width: 100%;
}
.dc-text-content > * {
margin-left: 2rem;
margin-right: 2rem;
}
.dc-text-content > p {
text-align: center;
margin-left: 15%;
margin-right: 15%;
}
JS
const iframe = document.querySelector('iframe');
const player = new Vimeo.Player(iframe);
Promise.all([player.getVideoWidth(), player.getVideoHeight()]).then(function(dimensions) {
let aspectRatio = dimensions[1]/dimensions[0];
const videoBox = document.querySelector(".dc-video-content");
const newVideoWidth = document.querySelector(".dc-video-row").offsetWidth/3
videoBox.style.width = newVideoWidth + "px";
videoBox.style.height = newVideoWidth * aspectRatio + "px";
iframe.width = newVideoWidth
iframe.height = newVideoWidth * aspectRatio;
});
解决方案
推荐阅读
- python - wxpython 改变图像大小和位置
- azure-sql-database - 接收器存储过程表名是必需的
- security - Jenkins Snyk 任务未发现通过 Snyk Web 扫描发现的漏洞
- c++ - 我可以重复使用 QRemoteObjectNode 吗?
- sql - liquibase 不支持以 postgres 作为数据库的“where not exists”子句吗?
- python - pyspark 计数和收集方法
- xml - 为什么属性也附加到子节点?
- python - 在 Django Rest Framework 中列出某些内容时,如何不仅仅显示对象的外键?
- android - Dagger 不允许我在一个类中使用两个相同类型的对象
- ios - 如何根据属性对对象数组进行升序排序 - Swift