html - 可缩放的全屏 BG 视频
问题描述
无论屏幕大小如何,我都试图让这个背景视频覆盖视口。如果屏幕足够宽,我可以正常工作:
我希望它覆盖视口。这是我的代码:
.video-background {
position: absolute;
top: 50%;
left: 0;
padding-top: 56.25%;
width: 100%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
-webkit-transition: 2s opacity ease;
transition: 2s opacity ease;
opacity: 1;
z-index: -1;
}
.video-foreground, .video-background iframe {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: -1;
}
<section class="full-height-outer home-banner loading" id="home-banner-box">
<div class="video-background">
<div class="video-foreground" id="YouTubeBackgroundVideoPlayer">
</div>
</div>
<div>
<H1 id="title" class="title">This is the title of my site</H1>
<p class="subtitle">And this is a great subtitle that describes what this page is all about.</p>
</div>
</section>
有小费吗?
解决方案
我认为问题在于我正在使用 YouTube 视频。大小在 JavaScript 中设置。使用 ojbect-fit:cover 只会将白条变成黑条。YouTube 播放器现在覆盖了屏幕,但视频的纵横比仍然会导致顶部和底部出现黑条。
目前,我修复它的方法是使 JavaScrpt 中的尺寸非常大,并调整 CSS。顶部和底部视频信息的负边距削减。我仍然觉得会有更好的方法来做到这一点。
你可以在这里看到现场版本:http: //spfict.com/
function onYouTubeIframeAPIReady() {
var player;
player = new YT.Player('YouTubeBackgroundVideoPlayer', {
videoId: 'rEagPJY9hwY', // YouTube Video ID
width: 2249, // Player width (in px)
height: 1265, // Player height (in px)
playerVars: {
playlist: 'rEagPJY9hwY',
autoplay: 1, // Auto-play the video on load
autohide: 1,
disablekb: 1,
controls: 0, // Hide pause/play buttons in player
showinfo: 0, // Hide the video title
modestbranding: 0, // Hide the Youtube Logo
loop: 1, // Run the video in a loop
fs: 0, // Hide the full screen button
autohide: 0, // Hide video controls when playing
rel: 0,
enablejsapi: 1
},
events: {
onReady: function(e) {
e.target.mute();
e.target.setPlaybackQuality('hd1080');
},
onStateChange: function(e) {
if(e && e.data === 1){
var videoHolder = document.getElementById('home-banner-box');
if(videoHolder && videoHolder.id){
videoHolder.classList.remove('loading');
}
}else if(e && e.data === 0){
e.target.playVideo()
}
}
}
});
}
.video-foreground, .video-background iframe {
position: fixed;
top: -60px;
min-width: 100%;
min-height: 100%;
pointer-events: none;
overflow: hidden;
display: flex;
z-index: -10;
-webkit-transition: 2s opacity ease;
transition: 2s opacity ease;
opacity: 1;
}
<section class="full-height-outer home-banner loading" id="home-banner-box">
<div class="video-foreground" id="YouTubeBackgroundVideoPlayer"></div>
<div>
<H1 id="title" class="title">This is the title of my site</H1>
<p class="subtitle">And this is a great subtitle that describes what this page is all about.</p>
</div>
</section>
推荐阅读
- c# - 网络应用程序中的 Cookie 被删除
- swift - selectItem 时的集合视图布局错误(Swift 5)
- swift - 在 String 中创建 NSRange 时崩溃
- php - 默认情况下,如何在 unirest for php 中将响应主体类型 stdObject 更改为关联数组
- xcode - 在 Xcode 中运行的 Build 和 Build 有什么区别
- firebase - Getter uid 在 Flutter Fire 中被调用为 null
- python - 无法让 Selenium WebDriver 在 Mac 上工作
- angular - 角度 6 到 8,角度通用,请通过“文件”或“包含”属性确保它在您的 tsconfig 中
- javascript - 如何在 sapper 上获得正确的 http 状态代码?
- c# - 如何查看播放音频文件的进度?