Ala*_*ano 11 html css video background youtube-iframe-api
你如何强制HTML5 iframeYouTube视频适合中心,使用CSS3 HTML覆盖全屏窗口背景最终Java?
例如" paypal.it "主页背景或" unity3d.com/5 "顶级视频,具有iframe youtube视频.所述iframe盖全屏(变焦)和覆盖所有的宽度和高度,当重新大小的窗口.它重新调整大小,保持100%最小宽度缩放高度或100%最小高度缩放宽度.
如何使用iframeHTML5和CSS3 实现这种效果?
代码示例HTML5
<div class="video" style="opacity: 1;">
<iframe src="http://www.youtube.com/embed/AddHereVideoId?autoplay=1&html5=1" frameborder="0" style="height: 720px;">
</iframe>
</div>
Run Code Online (Sandbox Code Playgroud)
代码CSS3 HTML最终将获得Java帮助.
Oli*_*ver 21
TL:DR -工作小提琴
作为对@Hinrich 答案的更新/改进,我创建了一个仅适用于所有纵横比的 CSS 缩放器 - 甚至是极端情况。而不是过补偿的宽度,以适应大多数的屏幕尺寸时,iframe使用设置vw和vh和偏移使用CSStransform属性(它抵消相对于元素,不是父)。
大多数浏览器(IE9+ 和所有常绿浏览器 AFAIK)都支持vw和vh单位,以及transforms,所以这应该适用于几乎所有不需要 JavaScript 的浏览器。
.video-background {
position: relative;
overflow: hidden;
width: 100vw;
height: 100vh;
}
.video-background iframe {
position: absolute;
top: 50%;
left: 50%;
width: 100vw;
height: 100vh;
transform: translate(-50%, -50%);
}
@media (min-aspect-ratio: 16/9) {
.video-background iframe {
/* height = 100 * (9 / 16) = 56.25 */
height: 56.25vw;
}
}
@media (max-aspect-ratio: 16/9) {
.video-background iframe {
/* width = 100 / (9 / 16) = 177.777777 */
width: 177.78vh;
}
}
Run Code Online (Sandbox Code Playgroud)
<div class="video-background">
<iframe src="https://www.youtube.com/embed/biWk-QLWY7U?controls=0&showinfo=0&rel=0&autoplay=1&loop=1&mute=1" frameborder="0" allowfullscreen></iframe>
</div>
Run Code Online (Sandbox Code Playgroud)
对于那些使用 CSS 变量的人,您也可以这样做 (fiddle),它允许您任意重用多个类名的大小:
:root {
--video-width: 100vw;
--video-height: 100vh;
}
@media (min-aspect-ratio: 16/9) {
:root {
--video-height: 56.25vw;
}
}
@media (max-aspect-ratio: 16/9) {
:root {
--video-width: 177.78vh;
}
}
.video-background {
position: relative;
overflow: hidden;
width: 100vw;
height: 100vh;
}
.video-background iframe {
position: absolute;
top: 50%;
left: 50%;
width: var(--video-width);
height: var(--video-height);
transform: translate(-50%, -50%);
}
Run Code Online (Sandbox Code Playgroud)
Hin*_*ich 10
对于真正的全屏解决方案,可以这样实现:
HTML
<div class="video-background">
<div class="video-foreground">
<iframe src="https://www.youtube.com/embed/I4agXcHLySs?controls=0&showinfo=0&rel=0&autoplay=1&loop=1&mute=1" frameborder="0" allowfullscreen></iframe>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS
* { box-sizing: border-box; }
.video-background {
background: #000;
position: fixed;
top: 0; right: 0; bottom: 0; left: 0;
z-index: -99;
}
.video-foreground,
.video-background iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
@media (min-aspect-ratio: 16/9) {
.video-foreground { height: 300%; top: -100%; }
}
@media (max-aspect-ratio: 16/9) {
.video-foreground { width: 300%; left: -100%; }
}
@media all and (max-width: 600px) {
.vid-info { width: 50%; padding: .5rem; }
.vid-info h1 { margin-bottom: .2rem; }
}
@media all and (max-width: 500px) {
.vid-info .acronym { display: none; }
}
Run Code Online (Sandbox Code Playgroud)
它并不完美,例如它不适用于容器的极高纵横比,但在大多数情况下都做得很好.这是一个工作示例:https: //codepen.io/anon/pen/zRVLGJ