【问题标题】:Html5 video background, keep center of video in centerHtml5视频背景,视频居中居中
【发布时间】:2015-01-08 22:13:00
【问题描述】:

无论用户拖动视频有多大,我都试图让背景视频居中。当我滚动较小时,它目前正在切断视频的右侧。这是我所拥有的:

<section id="home">
     <div class="video_shader"></div>
        <div class="video_contain">
            <video autoplay="" loop="" poster="img/still.jpg" id="bgvid">
              <source src="/realWebm.webm" type="video/webm" />
              <source src="/realdeal.mp4" type="video/mp4">
              <source src="/reaOg.ogv" type="video/ogg" />
            </video>
        </div>
</section>

.video_contain{
display: block;
position: absolute;
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
}

video {
min-width: 100%;
 min-height: 100%;
z-index: -100;
background-position: center;
background-size: cover;
}

#home {
width: 100vw;
height: 100vh;
display:block;
position: relative;
}

我希望视频的中心始终是页面的中心,即使两侧被切断 - 如果这样发生,那实际上是理想的。将不胜感激任何帮助。感谢阅读!

【问题讨论】:

    标签: css html video


    【解决方案1】:

    以下是我通常如何制作背景视频,以及我如何为 stre.am 着陆页制作:

    .video_contain {
        position: absolute;
        top: -50%;
        left: -50%;
        width: 200%;
        height: 200%;
    }
    
    video {
        position: absolute;
        top: 0;
        bottom: 0;
        right: 0;
        left: 0;
        margin: auto;
        min-height: 50%;
        min-width: 50%;
    }
    

    【讨论】:

    • 这很好用,直到我达到移动尺寸。有什么建议?谢谢!
    • 它在移动设备尺寸下的行为应该没有任何不同。确保您没有任何媒体查询样式干扰。我附上了一张手机尺寸应该是什么样子的图片。
    【解决方案2】:

    这要短得多,对我有用。

    video {
        position: fixed;
        right: 0;
        bottom: 0;
        min-width: 100%;
        min-height: 100%;
        transform: translateX(calc((100% - 100vw) / 2));
    }
    

    【讨论】:

    • 绝对是最佳答案@Pezcraft。就我而言,我只需要包含两行代码,它对我来说非常有效。非常感谢您的贡献。
    • 完美运行。我猜你从 W3Schools 获得了原始代码。我所做的只是添加了转换属性,我的视频完全按照我想要的方式工作。谢谢!
    • 这会在右侧创建一个奇怪的溢出(视频宽度 > 100%)。
    • 使用 Bootstrap 5 为我工作。谢谢!
    【解决方案3】:

    在我一直希望视频覆盖整个视口的用例中(无论视口纵横比是大于还是小于视频),上述解决方案并不能完全按照我的预期工作。相反,以下方法效果更好:

    .video-container {
      position: absolute;
      top: 0;
      left: 0;
      width: 100vw;
      height: 100vh;
    }
    .video-container > video {
      display: block;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      z-index: 1;
    }
    @media screen and (max-aspect-ratio: 1920/1080) {
      .video-container > video {
        height: 100%;
      }
    }
    @media screen and (min-aspect-ratio: 1920/1080) {
      .video-container > video {
        width: 100%;
      }
    }
    

    我的视频是 1920x1080,这适用于 IE11(未测试过更低)及更高版本。

    【讨论】:

    • 对我来说效果最好——而且,这是对媒体查询的极好使用!我添加了溢出:隐藏;到 .video-container 以防止在移动设备上向左/向右滚动
    • 我也会在 .video-container 上应用 position:fixed 。
    • 非常干净和强大
    • 我在.video-container &gt; video 选择器中添加了min-width: 100%min-height: 100%,将position 更改为relative,并为我们的视频通话工具添加了overflow: hidden。工作得很好。谢谢。
    • 这在其他人没有的地方奏效了。我根本不需要@media 选择器,left:50% 和 translate:-50% 是真正有效的。正如 Luchspeter 所说,它确实有助于隐藏容器上的溢出。
    【解决方案4】:
      .bg-video-wrap {
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
      }
    
      .bg-video-wrap > video,
      .bg-video-wrap > iframe {
        object-fit: cover;
        object-position: center center;
        width: 100%;
        height: 100%;
      }
    

    【讨论】:

    • 请在下次添加一些描述,代码在做什么
    • 甚至不需要包装。视频标签只有 4 行。谢谢!
    【解决方案5】:

    聚会迟到了,但我想给出一个 2020 年的答案。这是一个简单的解决方案,可让您拥有一个居中和响应式的 HTML 视频,而无需“固定”定位。它使您可以从全屏介绍开始,并在您开始滚动时添加一些文本。没有滚动条,没有烦人的东西。就这么简单。

    https://codepen.io/LuBre/pen/GRJVMqE?editors=1100

    CSS

    * {
      box-sizing: border-box;
    }
    body, html {
      margin: 0;
      height: 100%;
      font-Family: Arial;
    }
    
    .video-container {
      display: grid;
      justify-items: center;
      align-items: center;
      position: relative;
      width: 100%;
      height: 100%;
      overflow: hidden;
    }
    .video-container video {
      position: absolute;
      z-index: 1;
      top: 50%;
      left:50%;
      min-width: 100%;
      min-height: 100%;
      transform: translateX(-50%) translateY(-50%);
    }
    .video-text {
      z-index: 2; 
      color: #fff;
      text-align: center;
    }
    .video-container h1, .video-container h2  {
      margin: 0;
      font-size: 3rem;
    }
    .video-container h2  {
      font-size: 1.4rem;
      font-weight: normal;
      opacity: 0.6;
    }
    
    .page-content {
      line-height: 1.4rem;
      padding: 2rem;
    }
    

    HTML

    <div class="video-container">
      <video autoplay muted loop>
        <source src="https://www.w3schools.com/howto/rain.mp4" type="video/mp4">
      </video>
      <div class="video-text">
        <h1>Catchy title</h1>
        <h2>Everyone loves catchy titles</h2>
      </div>
    </div>
    
    <div class="page-content">
    <h1>New paragaph</h1>
    Some random text goes here...
    

    【讨论】:

    • 我正在研究这个视频内容并试图弄清楚......你如何将这样的东西变成旋转木马?只是想知道是否有简单的方法
    【解决方案6】:

    使用object-fit: cover;

    video {
      position: absolute;
      right: 0;
      bottom: 0;
      height: 100vh;
      width: 100vw;
      object-fit: cover;
    }
    

    【讨论】:

      【解决方案7】:

      就像任何其他具有绝对位置的元素一样居中

      .video_contain {
          position: absolute;
          width: auto;
          min-height: 100%;
          min-width: 100%;
          left: 50%;
          transform: translate(-50%);
      }
      

      【讨论】:

        【解决方案8】:

        这对我有用

        .video_contain {
          position: absolute;
          z-index: -1;
          top: 0px;
          left: 0px;
          bottom: 0px;
          right: 0px;
          overflow: hidden;
          background-size: cover;
          background-repeat: no-repeat;
          background-position: 50% 50%;
          background-image: none;
        }
        
        #bgvid {
          margin: auto;
          position: absolute;
          z-index: -1;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          visibility: visible;
          width: 1267px;
          height: auto;
        }
        

        【讨论】:

          【解决方案9】:

          这对我有用,始终保持视频居中,而不用担心视频的实际尺寸

          .video_contain {
            position: absolute;
            top: 0;
            left: 0;
            /** could be any size **/
            width: 100%;
            height: 100%;
            overflow: hidden;
            z-index: 0;
          }
          video {
            display: block;
            min-height: 100%;
            min-width: 100%;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 1;
          }
          

          【讨论】:

            【解决方案10】:

            所以我测试了上述解决方案,但找不到那个,所以这是我的:

            video {
                  position: fixed;
                  left: 50%;
                  top: 50%;
                  min-width: 100%;
                  min-height: 100%;
                  transform: translate(50%, -50%);
            }
            

            【讨论】:

              猜你喜欢
              • 2023-03-03
              • 2018-05-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-10-06
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多