【问题标题】:use video as background for div使用视频作为 div 的背景
【发布时间】:2014-01-16 02:51:41
【问题描述】:

我想在 CSS3 中使用视频作为背景。我知道没有 background-video 属性,但是可以做到这一点。使用全尺寸视频标签不会产生想要的结果,因为需要在视频上显示内容。

必须是非 JS。如果不可能,那么我需要在我的服务器端进行更改,并提供视频截图。

我需要视频来替换彩色框:

彩色框只是 atm,CSS 框。

【问题讨论】:

标签: html css html5-video


【解决方案1】:

纯 CSS 方法

可以使用object-fit 属性或CSS Transforms 将视频置于元素内部,就像cover 大小为background-image 没有JS

2021 答案:对象匹配

正如 cmets 中所指出的,不使用 CSS transform 也可以实现相同的结果,但使用 object-fit,我认为这是获得相同结果的更好选择:

.video-container {
    height: 300px;
    width: 300px;
    position: relative;
}

.video-container video {
  width: 100%;
  height: 100%;
  position: absolute;
  object-fit: cover;
  z-index: 0;
}

/* Just styling the content of the div, the *magic* in the previous rules */
.video-container .caption {
  z-index: 1;
  position: relative;
  text-align: center;
  color: #dc0000;
  padding: 10px;
}
<div class="video-container">
    <video autoplay muted loop>
        <source src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4" />
    </video>
    <div class="caption">
      <h2>Your caption here</h2>
    </div>
</div>

上一个答案:CSS 变换

借助transform CSS 属性,您可以轻松地将视频设置为任何 HTML 元素的背景。

请注意,您可以使用transform 技术将任何 HTML 元素垂直和水平居中。

.video-container {
  height: 300px;
  width: 300px;
  overflow: hidden;
  position: relative;
}

.video-container video {
  min-width: 100%;
  min-height: 100%;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
}

/* Just styling the content of the div, the *magic* in the previous rules */
.video-container .caption {
  z-index: 1;
  position: relative;
  text-align: center;
  color: #dc0000;
  padding: 10px;
}
<div class="video-container">
  <video autoplay muted loop>
    <source src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4" />
  </video>
  <div class="caption">
    <h2>Your caption here</h2>
  </div>
</div>

【讨论】:

  • 这是聪明的想法。我能够将 scale(2) 添加到转换中,并将 overflow: hidden; 添加到容器 div 以实现我正在寻找的全尺寸响应式剪辑视频背景。
  • 这其实是这个问题的正确答案!
  • @vars 你可以使用 object-fit:cover;在视频标签上使其全屏显示。这样您也可以轻松定位视频(对象位置:中心中心;)
  • 我必须添加以下内容才能使其正常工作:宽度:100%;高度:100%;适合对象:封面;
  • 要走的路:宽度:100%;高度:100%;适合对象:封面; object-position: center center;
【解决方案2】:

为什么不修复&lt;video&gt; 并使用z-index:-1 将其放在所有其他元素的后面?

html, body { width:100%; height:100%; margin:0; padding:0; }

<div style="position: fixed; top: 0; width: 100%; height: 100%; z-index: -1;">
    <video id="video" style="width:100%; height:100%">
        ....
    </video>
</div>
<div class='content'>
    ....

Demo

如果你想把它放在一个容器中,你必须添加一个容器元素和更多的 CSS

/* HTML */
<div class='vidContain'>
    <div class='vid'>
        <video> ... </video>
    </div>
    <div class='content'> ... The rest of your content ... </div>
</div>

/* CSS */
.vidContain {
    width:300px; height:200px;
    position:relative;
    display:inline-block;
    margin:10px;
}
.vid {
    position: absolute; 
    top: 0; left:0;
    width: 100%; height: 100%; 
    z-index: -1;
}    
.content {
    position:absolute;
    top:0; left:0;
    background: black;
    color:white;
}

Demo

【讨论】:

  • 不起作用,我需要视频来替换彩色框,请参阅我的编辑 :)
  • @Knerd 所以使用相同的方法,但添加一个容器。我更新了我的答案
  • 哦,对不起,我又忘记了,但它现在可以工作了 :) 非常感谢 :) 我的网格中只有一个填充,但没关系。
  • 在 Chrome 中看起来坏了。
  • @zehelvion 它仍然可以正常工作。视频不可点击,但这不是问题的一部分。您可以添加autoplay 属性来查看视频是否正常工作。
【解决方案3】:

我相信这就是您正在寻找的。它会自动缩放视频以适应容器。

演示:http://jsfiddle.net/t8qhgxuy/

视频需要始终将高度和宽度设置为父级的 100%。

HTML:

<div class="one"> CONTENT OVER VIDEO
    <video class="video-background" no-controls autoplay src="https://dl.dropboxusercontent.com/u/8974822/cloud-troopers-video.mp4" poster="http://thumb.multicastmedia.com/thumbs/aid/w/h/t1351705158/1571585.jpg"></video>
</div>

<div class="two">
    <video class="video-background" no-controls autoplay src="https://dl.dropboxusercontent.com/u/8974822/cloud-troopers-video.mp4" poster="http://thumb.multicastmedia.com/thumbs/aid/w/h/t1351705158/1571585.jpg"></video> CONTENT OVER VIDEO
</div>

CSS:

body {
    overflow: scroll;
    padding:  60px 20px;
}

.one {
    width: 90%;
    height: 30vw;
    overflow: hidden;
    border: 15px solid red;
    margin-bottom: 40px;
    position: relative;
}

.two{
    width: 30%;
    height: 300px;
    overflow: hidden;
    border: 15px solid blue;
    position: relative;
}

.video-background { /* class name used in javascript too */
    width: 100%; /* width needs to be set to 100% */
    height: 100%; /* height needs to be set to 100% */
    position: absolute;
    left: 0;
    top: 0;
    z-index: -1;
}

JS:

function scaleToFill() {
    $('video.video-background').each(function(index, videoTag) {
       var $video = $(videoTag),
           videoRatio = videoTag.videoWidth / videoTag.videoHeight,
           tagRatio = $video.width() / $video.height(),
           val;

       if (videoRatio < tagRatio) {
           val = tagRatio / videoRatio * 1.02; <!-- size increased by 2% because value is not fine enough and sometimes leaves a couple of white pixels at the edges -->
       } else if (tagRatio < videoRatio) {
           val = videoRatio / tagRatio * 1.02;
       }

       $video.css('transform','scale(' + val  + ',' + val + ')');

    });    
}

$(function () {
    scaleToFill();

    $('.video-background').on('loadeddata', scaleToFill);

    $(window).resize(function() {
        scaleToFill();
    });
});

【讨论】:

  • 不使用 JavaScript 是否可用?我想要一个纯 HTML 和 JS 的解决方案。
猜你喜欢
  • 1970-01-01
  • 2019-03-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2014-10-19
  • 2016-02-26
  • 1970-01-01
  • 2016-09-02
相关资源
最近更新 更多