Video.js 是一个开源的 HTML5 视频播放器,它允许开发者通过简单的标记和 JavaScript 来创建自定义的视频播放器界面。在 Video.js 中,width
属性用于设置视频播放器的宽度。
width="640"
。width="100%"
。以下是一个简单的 Video.js 播放器设置宽度的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Video.js Example</title>
<link href="https://vjs.zencdn.net/7.14.3/video-js.css" rel="stylesheet" />
</head>
<body>
<video id="my-video" class="video-js vjs-default-skin" controls preload="auto" width="640" height="264">
<source src="my-video.mp4" type="video/mp4" />
Your browser does not support the video tag.
</video>
<script src="https://vjs.zencdn.net/7.14.3/video.js"></script>
<script>
var player = videojs('my-video');
</script>
</body>
</html>
问题:视频播放器宽度设置后没有变化。
原因:
解决方法:
/* 确保没有其他样式覆盖 */
.video-js {
width: 100% !important;
}
// 确保 Video.js 初始化代码在 DOM 完全加载后执行
document.addEventListener('DOMContentLoaded', function() {
var player = videojs('my-video');
});
通过以上方法,可以有效解决视频播放器宽度设置不正确的问题。
领取专属 10元无门槛券
手把手带您无忧上云