在背景上设置YouTube视频

时间:2017-02-05 17:39:17

标签: html css twitter-bootstrap video youtube

我正在使用bootstrap,我想在背景上设置youtube视频

我找到了这个。如何更改此代码以使用来自youtube的视频?替换不起作用

    @import url('http://fonts.googleapis.com/css?family=Oswald');
    
    body {
      margin: 0;
      padding: 0;
      background-attachment: fixed;
      background-size: cover;
    }
    
    #video-background {
      position: fixed;
      right: 0; 
      bottom: 0;
      min-width: 100%; 
      min-height: 100%;
      width: auto; 
      height: auto;
      z-index: -100;
    }
    <video autoplay="" loop="" class="fillWidth fadeIn animated" poster="https://s3-us-west-2.amazonaws.com/coverr/poster/Traffic-blurred2.jpg" id="video-background">
        <source src="https://s3-us-west-2.amazonaws.com/coverr/mp4/Traffic-blurred2.mp4" type="video/mp4">
    </video>

5 个答案:

答案 0 :(得分:4)

<div style="position: fixed; z-index: -99; width: 100%; height: 100%"> <iframe frameborder="0" height="100%" width="100%" src="https://youtube.com/embed/ID?autoplay=1&controls=0&showinfo=0&autohide=1"> </iframe> </div> 中添加这些行。更改ID号。

{{1}}

答案 1 :(得分:3)

我找到了很好的解决方案here

.video-background {
  background: #000;
  position: fixed;
  top: 0; right: 0; bottom: 0; left: 0;
  z-index: -99;
}
.video-foreground,
.video-background iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

答案 2 :(得分:2)

使用Youtube API + CSS,我们可以构建全屏youtube视频背景。

HTML标记:

<div class="page">
  <div class="video-bg">
    <div id="videoPlayer" data-videoid="b0r_dH7jfOM"></div>
  </div>
  <div class="content">
    <h1>Video Player background</h1>
    <h3>Full screen youtube video player in background.</h3>
  </div>
</div>

CSS:

.page {
 position: relative;
 overflow: hidden;
}

.page .video-bg {
 width: 100%;
 height: 0;
 padding-bottom: 56.25%;/* Aspect ratio */
 position: absolute;
}

.page .video-bg iframe {
 border: 0;
 position: absolute;
 top: 0;
 left: 0;
 width: 100%;
 height: 100%;
}

.page .content {
 position: relative;
 z-index: 1;
}

添加Youtube API:https://www.youtube.com/iframe_api

JS:

jQuery(function () {

// Youtube player
window.videoPlayer;

window.onYouTubeIframeAPIReady = function () {
 var videoPlayerId = $('#videoPlayer').attr('data-videoid');
 window.videoPlayer = new YT.Player('videoPlayer', {
  height: '1080',
  width: '1920',
  videoId: videoPlayerId,
  playerVars: {
   'controls': 0,
   'autoplay': 1,
   'mute': 1,
   'loop': 1,
   'showinfo': 0,
   'modestbranding': 1
  },
  events: {
   'onReady': onVideoPlayerReady,
   'onStateChange': onVideoPlayerReady
  }
 });
}

function onVideoPlayerReady(event) {
 videoPlayer.playVideo();
}
});

答案 3 :(得分:2)

嗨,大家好,这是我基于YouTube Embed API开发的可重复使用的简单jQuery插件。使用起来也超级简单。您可以在操作here中看到它。

<div id="ytbg" data-youtube="https://www.youtube.com/watch?v=eEpEeyqGlxA"></div>

<script type="text/javascript">
    jQuery(document).ready(function() {
        $('[data-youtube]').youtube_background();
    });
</script>

GitHub上的代码

答案 4 :(得分:1)

您可以使用来自youtube的嵌入视频,只需输入id =“video-background” 并删除高度/宽度

<iframe id="video-background" src="https://www.youtube.com/embed/BrCKvKXvN2c?list=RDgfwMBzGA_Jo" frameborder="0" allowfullscreen></iframe>

Explained HERE