php - 使用 API 动态拉伸(stretch)以适合 YouTube 视频

标签 php html css youtube

我想重新创建这个:DEMO

HTML

<header role="banner">
 <div id="wrapper-video">
<video poster="" autoplay loop>
  <source src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/156843/cut.mp4"           
    type="video/mp4; codecs=avc1.42E01E,mp4a.40.2">
</video>
</div>
</header>

CSS

section[role="banner"] {
 position: relative;
 width: 100%;
 }

#wrapper-video {
 position: fixed;
 top: -50%;
 left: -50%;
 width: 200%;
 height: 200%;
 z-index: -100;
 }

#wrapper-video video {
 position: absolute;
 top: 0;
 left: 0;
 right: 0;
 bottom: 0;
 margin: auto;
 min-width: 50%;
 min-height: 50%;
 }

...但将视频替换为通过 YouTube API 加载的 YouTube 视频,它将播放器加载到 DIV 中,如下所示:

<div id="player"></div>

但是,一旦我开始更改 html,居中就会关闭,即使我将上面的内容更改为图像(作为概念证明),居中也会关闭。我在这里做错了什么?

如果我将#wrapper-video video 更改为#wrapper-video2 并重命名它所在的 DIV,它是否会出现意外情况?

最佳答案

#player 将替换为 iframe。以下是如何将响应代码与 youtube API 结合使用。

var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

function onYouTubePlayerAPIReady() {
  player = new YT.Player('player', {
    height: '360',
    width: '640',
    videoId: 'M7lc1UVf-VE'
  });
}
section[role="banner"] {
  position: relative;
  width: 100%;
}

#wrapper-video {
  position: fixed;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  z-index: -100;
}

#wrapper-video video,
#wrapper-video iframe {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  min-width: 50%;
  min-height: 50%;
}
<header role="banner">
  <div id="wrapper-video">
    <div id="player"></div>
  </div>
</header>

关于php - 使用 API 动态拉伸(stretch)以适合 YouTube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43598284/

相关文章:

PHP file_get_contents() SSL 操作失败,代码为 1

html - 这个导航栏和边框怎么做?

html - 推特 Bootstrap : What would be the best way to add a background color to the side margins but not the main content?

html - CSS 样式表定义的样式未转换为演示中的 HTML 网页(Visual Studio 2017)

javascript - 如何使用 jquery 设置多个 CSS 显示属性值

php - 如何从mysql中的日期获取月份

php - gearman doBackground() 不使用 php

php - 如何在登录时向 symfony2 上的 session 注入(inject)特定值?

html - 使用 flex 和 border-box 等宽

php - 如何将 HTML 格式添加到 'Swift Mail tutorial based' PHP 电子邮件?