【问题标题】:How to put a background video loop on a website HTML/CSS如何在网站 HTML/CSS 上放置背景视频循环
【发布时间】:2014-06-18 18:55:45
【问题描述】:

我正在尝试将视频作为背景视频放在我的网站上。我研究了很多,但它不起作用,我不知道为什么。

在我的 html 中,我有:

<video id="bgVideo" controls preload="true" autoplay loop muted>
    <source src="../images/Home_Page.mp4" type="video/mp4" >
    <source src="../images/Home_Page.ogv" type="video/ogg" >    
    <source src="../images/Home_Page.webm" type="video/webm" > 
</video>     
<script src="scripts/html5ext.js" type="text/javascript"></script> 

在我的 CSS 上我有:

body
{
background:  url("../images/Home_Page.png") no-repeat fixed center;
}

video#bgVideo {
position: fixed;
right: 0;
bottom: 0;
width: auto;
min-width: 100%;
height: auto;
min-height: 100%;
z-index: -100;    
background-size: cover;
}

但是,视频没有播放并且背景保持白色。有什么解决这个问题的建议吗??

非常感谢你

【问题讨论】:

    标签: html css video


    【解决方案1】:

    这应该可以工作

       <video id="bgVideo" controls preload="true" autoplay loop muted>
          <source src="Home_Page.mp4" type="video/mp4" > 
           <source src="Home_Page.ogv" type="video/ogv" >    
          <source src="Home_Page.webm" type="video/webm" > 
    
       </video>
    

    您还应该创建一个 htaccess 文件/修改您的 htaccess 文件以包含以下内容

       AddType video/mp4 .mp4 .m4v
       AddType video/ogg .ogv
       AddType video/webm .webm
    

    一个有用的教程是here

    【讨论】:

    • 背景是home_page.png为什么还需要海报?
    • 您收到的错误是已知的 Mozilla 错误。它适用于重新加载
    • 查看此链接以获取有关错误的更多信息 - 我会将 mp4 放在第一位(来源)coolestguidesontheplanet.com/…
    • 我现在正在尝试使用 chrome rigth,但视频无法播放。它只是在页面底部显示一个视频控制器,但是当我点击播放时它不会播放
    • 您是否预加载了 ="true"(不是自动)?
    【解决方案2】:
            <div class="video-container">
    
                <video autoplay loop muted width="100%">
                    <source src="Path To mp4 Video" type="video/mp4" />
                    <source src="Path To webm Video" type="video/webm" />
                </video>                
                <div >
                    <img src="path to Image" alt="">
                </div>
                <!-- If Videos are not supported this image will act as cover image-->
            </div>
    

    【讨论】:

      【解决方案3】:

      此代码对我有用,它提供全屏视频。

      var vid = document.getElementById("bgvid");
      var pauseButton = document.querySelector("#polina button");
      
      if (window.matchMedia('(prefers-reduced-motion)').matches) {
          vid.removeAttribute("autoplay");
          vid.pause();
          pauseButton.innerHTML = "Paused";
      }
      
      function vidFade() {
        vid.classList.add("stopfade");
      }
      
      vid.addEventListener('ended', function()
      {
      // only functional if "loop" is removed 
      vid.pause();
      // to capture IE10
      vidFade();
      }); 
      
      
      pauseButton.addEventListener("click", function() {
        vid.classList.toggle("stopfade");
        if (vid.paused) {
          vid.play();
          pauseButton.innerHTML = "Pause";
        } else {
          vid.pause();
          pauseButton.innerHTML = "Paused";
        }
      })
      body {
        margin: 0;
        background: #000; 
      }
      video { 
          position: fixed;
          top: 50%;
          left: 50%;
          min-width: 100%;
          min-height: 100%;
          width: auto;
          height: auto;
          z-index: -100;
          transform: translateX(-50%) translateY(-50%);
       background: url('//demosthenes.info/assets/images/polina.jpg') no-repeat;
        background-size: cover;
        transition: 1s opacity;
      }
      .stopfade { 
         opacity: .5;
      }
      
      #polina { 
        font-family: Agenda-Light, Agenda Light, Agenda, Arial Narrow, sans-serif;
        font-weight:100; 
        background: rgba(0,0,0,0.3);
        color: white;
        padding: 2rem;
        width: 33%;
        margin:2rem;
        float: right;
        font-size: 1.2rem;
      }
      h1 {
        font-size: 3rem;
        text-transform: uppercase;
        margin-top: 0;
        letter-spacing: .3rem;
      }
      #polina button { 
        display: block;
        width: 80%;
        padding: .4rem;
        border: none; 
        margin: 1rem auto; 
        font-size: 1.3rem;
        background: rgba(255,255,255,0.23);
        color: #fff;
        border-radius: 3px; 
        cursor: pointer;
        transition: .3s background;
      }
      #polina button:hover { 
         background: rgba(0,0,0,0.5);
      }
      
      a {
        display: inline-block;
        color: #fff;
        text-decoration: none;
        background:rgba(0,0,0,0.5);
        padding: .5rem;
        transition: .6s background; 
      }
      a:hover{
        background:rgba(0,0,0,0.9);
      }
      @media screen and (max-width: 500px) { 
        div{width:70%;} 
      }
      @media screen and (max-device-width: 800px) {
        html { background: url(https://thenewcode.com/assets/images/polina.jpg) #000 no-repeat center center fixed; }
        #bgvid { display: none; }
      }
      <video poster="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/polina.jpg" id="bgvid" playsinline autoplay muted loop>
        <!-- WCAG general accessibility recommendation is that media such as background video play through only once. Loop turned on for the purposes of illustration; if removed, the end of the video will fade in the same way created by pressing the "Pause" button  -->
      <source src="http://thenewcode.com/assets/videos/polina.webm" type="video/webm">
      <source src="http://thenewcode.com/assets/videos/polina.mp4" type="video/mp4">
      </video>
      <div id="polina">
      <h1>try this</h1>
      
      <p>your text or elements</p>
         
      <button>Pause</button>
      </div>

      【讨论】:

        猜你喜欢
        • 2023-02-15
        • 1970-01-01
        • 2016-06-04
        • 2013-02-10
        • 2011-04-17
        • 1970-01-01
        • 2017-05-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多