首页 > 解决方案 > AMP整页封面背景视频

问题描述

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

我已经尝试过变体,layout='responsive'但在页面加载之前我不知道页面的高度,并且我无法在页面加载后动态更新width或支持。height我本质上是在模仿object-fitCSS 道具。object-fit: cover显然是由 AMP CSS 支持的。

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

<!doctype html>
<html ⚡&gt;
<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>

标签: cssamp-html

解决方案


我想这就是你要找的。

如果需要,视频在背景中,具有固定位置,内容在前景中。

<!doctype html>
<html ⚡&gt;

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



推荐阅读