AMP整页封面背景视频

时间:2018-05-21 16:51:54

标签: css amp-html

我是AMP开发的新手,无法绕过布局。我正在尝试创建整页封面背景视频。

我尝试了layout='responsive'的变种,但我不知道页面加载前页面的高度和AFAIK我无法在页面后动态更新widthheight道具加载。我基本上是在模仿object-fit CSS道具。 AMP CSS显然支持object-fit: cover

我尝试在下面的代码段中使用object-fit,但无济于事。在代码段中,使用video div中的普通HTML5 expected标记显示预期行为。

<!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>

1 个答案:

答案 0 :(得分:3)

我认为这就是你要找的东西。

如果需要,视频在后台固定定位,内容在前景中。

<!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>