How to put a background video loop on a website HTML/CSS

前端 未结 3 1398
情歌与酒
情歌与酒 2021-02-04 15:58

I\'m trying to put a video as a background video on my website. I\'ve researched a lot and it is not working and I don\'t know why.

On my html I have:

&l         


        
3条回答
  •  -上瘾入骨i
    2021-02-04 16:35

    This code is worked for me its providing full screen video.

    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; }
    }
    
    

    try this

    your text or elements

提交回复
热议问题