Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AMP full page cover background video

Tags:

css

amp-html

I am a newbie to AMP development and can't wrap my head around the layouts. I am trying to create full page cover background video.

I have tried variants of layout='responsive' but I won't know the height of the page prior to page load and AFAIK I can't update width or height prop dynamically after page load. I am essentially emulating the object-fit CSS prop. object-fit: cover is apparently supported by AMP CSS.

I tried using object-fit in the snippet below but to no avail. In the snippet the expected behaviour is displayed using normal HTML5 video tag in the expected div.

<!doctype html>
<html ⚡>
<head>
  <meta charset="utf-8">
  <title>My AMP Page</title>
  <link rel="canonical" href="self.html" />
  <meta name="viewport" content="width=device-width,minimum-scale=1">
  <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
  <script async src="https://cdn.ampproject.org/v0.js"></script>
  <script async custom-element="amp-video" src="https://cdn.ampproject.org/v0/amp-video-0.1.js"></script>
  <style amp-custom>
    * {
      box-sizing: border-box;
    }
     .container {
      height: 100vh;
      width: 100vw;
      border: 1px solid red;
      position: relative;
    }
    .video {
        object-fit: cover;
        height: 100vh; 
        position: absolute; 
    } 
    h1 {
      position: absolute;
      top: 0;
      left: 0;
      z-index: 2;
    }
    .expected {
      display: block;
    }
  </style>
</head>
<body>
    <div class='container'>
        <amp-video layout="fill"
            autoplay="autoplay" muted loop preload="auto"
            class="video">
            <source src='https://davidalbertoadler.com/assets/img/Fish-Tank/MP4/Fish-Tank.mp4'>
      </amp-video>
    </div>

    <div class="container expected">
        <h1>Expected</h1>
        <video layout='fill'
            autoplay="autoplay" muted loop preload="auto"
            class="video">
            <source src='https://davidalbertoadler.com/assets/img/Fish-Tank/MP4/Fish-Tank.mp4'>
        </video>
     </div>
</body>
</html>
like image 908
david_adler Avatar asked Oct 16 '22 18:10

david_adler


1 Answers

I think this is what you're looking for.

The video is in the background with fixed positioning with content in the foreground if desired.

<!doctype html>
<html ⚡>

<head>
  <meta charset="utf-8">
  <title>My AMP Page</title>
  <link rel="canonical" href="self.html" />
  <meta name="viewport" content="width=device-width,minimum-scale=1">
  <style amp-boilerplate>
    body {
      -webkit-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
      -moz-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
      -ms-animation: -amp-start 8s steps(1, end) 0s 1 normal both;
      animation: -amp-start 8s steps(1, end) 0s 1 normal both
    }
    
    @-webkit-keyframes -amp-start {
      from {
        visibility: hidden
      }
      to {
        visibility: visible
      }
    }
    
    @-moz-keyframes -amp-start {
      from {
        visibility: hidden
      }
      to {
        visibility: visible
      }
    }
    
    @-ms-keyframes -amp-start {
      from {
        visibility: hidden
      }
      to {
        visibility: visible
      }
    }
    
    @-o-keyframes -amp-start {
      from {
        visibility: hidden
      }
      to {
        visibility: visible
      }
    }
    
    @keyframes -amp-start {
      from {
        visibility: hidden
      }
      to {
        visibility: visible
      }
    }
  </style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
  <script async src="https://cdn.ampproject.org/v0.js"></script>
  <script async custom-element="amp-video" src="https://cdn.ampproject.org/v0/amp-video-0.1.js"></script>
  <style amp-custom>
    * {
      box-sizing: border-box;
    }
    
    .background {
      height: 100vh;
      width: 100vw;
      border: 1px solid red;
      position: fixed;
    }
    
    .content-holder {
      position: relative;
      background-color: #fff;
      max-width: 250px;
      margin: auto;
      min-height: 2000px;
      padding: 10px;
    }
    
    .content {
      position: relative;
    }
  </style>
</head>

<body>
  <div class='background'>
    <amp-video width="480" height="270" src='https://davidalbertoadler.com/assets/img/Fish-Tank/MP4/Fish-Tank.mp4' autoplay="autoplay" layout="responsive">
      <source src='https://davidalbertoadler.com/assets/img/Fish-Tank/MP4/Fish-Tank.mp4'>
    </amp-video>
  </div>
  <div class="content-holder">
    <div class="content">
      <h1>This is content</h1>
    </div>
  </div>
</body>

</html>

like image 198
TidyDev Avatar answered Oct 20 '22 00:10

TidyDev