背景视频 css(非全屏)

background video css (not fullscreen)

大家好! 我有一个关于背景视频的问题,我把它附加到整个页面,但只需要将它嵌入到顶部。谁能告诉我有什么问题? 非常感谢! HTML:

<video autoplay loop poster="video-bg.jpg" id="bgvid">
    <source src="video-bg.webm" type="video/webm">
    <source src="video-bg.mp4" type="video/mp4">
</video>

CSS:

video#bgvid { 
    position: fixed;
    top: 50%;
    left: 50%;
    min-width: 100%;
    min-height: 100%;
    width: auto;
    height: auto;
    z-index: -100;
    -ms-transform: translateX(-50%) translateY(-50%);
    -moz-transform: translateX(-50%) translateY(-50%);
    -webkit-transform: translateX(-50%) translateY(-50%);
    transform: translateX(-50%) translateY(-50%);
    background: url(video-bg.jpg) no-repeat;
    background-size: cover; 
}
@media screen and (max-device-width: 800px) {
    html {
         background: url(video-bg.jpg) #000 no-repeat center center fixed;
    }
    #bgvid {
        display: none;
    }
}

位置是固定的,所以它会固定到整个页面。使用 position: absolute; 同时更改高度和宽度。

先删除min-width/height: 100%;
transform: translateX(-50%) translateY(-50%); 替换为 transform: translateX(-50%); 以仅水平移动元素
最后把top: 50%;改成top: 0;

如果您希望在滚动页面时也滚动视频,请将 position: fixed 替换为 position: absolute;

#bgvid {
  background: none #aaa !important;
  text-align: center;
  width: 300px;
  height: 150px;
}
#bgvid {
    position: fixed;
    top: 0;
    left: 50%;
    /*min-width: 100%;*/
    /*min-height: 100%;*/
    -ms-transform: translateX(-50%);
    -moz-transform: translateX(-50%);
    -webkit-transform: translateX(-50%);
    transform: translateX(-50%);
    z-index: -100;
    background: url(video-bg.jpg) no-repeat;
    background-size: cover; 
}
@media screen and (max-device-width: 800px) {
    html {
         background: url(video-bg.jpg) #000 no-repeat center center fixed;
    }
    #bgvid {
        display: none;
    }
}
<div id="bgvid">VIDEO HERE</div>