html - 视频作为背景视差 CSS

标签 html css video parallax

我是后端而不是前端,所以如果你认为我的问题是假的,很抱歉,但我找不到答案:)

我的目标是使用 CSS 制作具有视差的背景视频。总的来说,我确实做到了,但结果还不够好。由于某种原因,背景中的视频在所有部分下,而不是只在它应该在的一个部分中......

HTML:

<div class="fullScreenPhoto"></div>
<div class="video-container">
  <video autoplay poster="" class="video-parallax" loop muted>
    <source src="https://showbox-tr.dropbox.com/transcode_video/t/1qz2fy47wt9ay7i/header_background.mp4" type="video/webm">
    <source src="https://showbox-tr.dropbox.com/transcode_video/t/1qz2fy47wt9ay7i/header_background.mp4" type="video/mp4">
  </video>
</div>

<div class="fullScreenPhoto2"></div>

<div class="fullScreenPhoto3"></div>

CSS:

body{
  margin: 0;
  padding:0
}

.fullScreenPhoto{
  width: 100%;
  margin:0;
  height:300px;
  background-color: red;
}

.fullScreenPhoto2{
  width: 100%;
  height:300px;
    margin:0;
  background-color: green;
}
.fullScreenPhoto3{
  width: 100%;
    margin:0;
  height:300px;
  background-color: yellow;
}

video {
    top: 50%;
    left: 50%;
    min-width: 100%;
    min-height: 100%;
    width: auto;
    height: auto;
    z-index: -100;
    transform: translateX(-50%) translateY(-50%);
    background-size: cover;
    transition: 1s opacity;
}

.video-container {
    height: 600px;
}

.video-parallax {
    -webkit-transition-position: fixed;
    position: fixed;
}

在这里你可以摆弄: Fiddle

我在哪里重复了我的问题。如果您要隐藏一个部分,或将 z-index 更改得更高,您会看到该视频遍布整个页面...

顺便说一句。我知道插件 jQuery -> https://github.com/linnett/backgroundVideo ,但我只想使用 CSS。

最佳答案

您可以像处理图像视差一样轻松地做到这一点。

HTML:

<div class="ParallaxVideo">
  <video autoplay muted loop>
  <source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
  <source src="http://www.w3schools.com/html/mov_bbb.ogg" type="video/ogg">
  </video>
  <h1>Video Background</h1>
</div>

CSS

.ParallaxVideo{ 
    height: 300px;
    padding-bottom: 50px;
    padding-top: 50px; 
}
.ParallaxVideo video{ 
    min-width: 100%;
    position: fixed;
    top:0;
    z-index: -999;
}
.ParallaxVideo h1 {
  color: #fff;
  font-size: 76px;
  font-weight: 700;
  text-align: center;
  text-transform: uppercase;
}

您还可以查看本教程以获取更多详细信息:

https://codeconvey.com/video-parallax-background-using-css3/

这是演示版:

http://codeconvey.com/Tutorials/VideoParallaxBackground/

关于html - 视频作为背景视差 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41667536/

相关文章:

java - CssResource 只定义了部分类?

html - 为什么我的背景显示在 Qualtrics 的页脚下?

html - 包含文本的部分与包含无序列表的部分重叠

html - 如何使用批处理文件在 HTML 文件中写入 "greater than symbol"

css - 添加backgroundcolor后伪li元素消失

android - 如何从 YouTube URL 获取视频缩略图并将其设置为 android 中的 ImageView

wpf - 是否可以使用 WPF 在多台计算机之间同步视频?

java - 使用 ffmpeg 生成视频以使用 JavaFX 播放

html - 中等黑色圆圈在某些设备上始终为黑色

jquery - 导航栏告诉您您所在的位置