video as site background? HTML 5

VideoHtmlBackgroundHtml5 Video

Video Problem Overview


I want to use a video as a background instead of an image that automatically stretches to the whole screen (background).

I would also like to rotate videos and images.. so that there is a random video/image displayed in any order.

It would also be nice to know how to delay video playback, so that the video only plays once 30 seconds after the site loaded.

thx!

Video Solutions


Solution 1 - Video

Take a look at my jquery videoBG plugin

http://syddev.com/jquery.videoBG/

Make any HTML5 video a site background... has an image fallback for browsers that don't support html5

Really easy to use

Let me know if you need any help.

Solution 2 - Video

First, your HTML markup looks like this:

<video id="awesome_video" src="first_video.mp4" autoplay />

Second, your JavaScript code will look like this:

<script type="text/javascript">
  var index = 1,
      playlist = ['first_video.mp4', 'second_video.mp4', 'third_video.mp4'],
      video = document.getElementById('awesome_video');
  
  video.addEventListener('ended', rotate_video, false);
  
  function rotate_video() {
    video.setAttribute('src', playlist[index]);
    video.load();
    index++;
    if (index >= playlist.length) { index = 0; }
  }
</script>

And last but not least, your CSS:

#awesome_video { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }

This will create a video element on your page that starts playing the first video right away, then iterates through the playlist defined by the JavaScript variable. Your mileage with the CSS may vary depending on the CSS for the rest of the site, but 100% width/height should do it on a basic page.

Solution 3 - Video

I might have a solution for the video as background, stretched to the browser-width or height, (but the video will still preserve the aspect ratio, couldnt find a solution for that yet.):

Put the video right after the body-tag with style="width:100%;". Right afterwords, put a "bodydummy"-tag:

<body>
<video id="bgVideo" autoplay poster="videos/poster.png">
	<source src="videos/test-h264-640x368-highqual-winff.mp4" type="video/mp4"/>
	<source src="videos/test-640x368-webmvp8-miro.webm" type="video/webm"/>
	<source src="videos/test-640x368-theora-miro.ogv" type="video/ogg"/>	
</video>

<img id="bgImg" src="videos/poster.png" />

<!-- This image stretches exactly to the browser width/height and lies behind the video-->

<div id="bodyDummy">

Put all your content inside the bodydummy-div and put the z-indexes correctly in CSS like this:

#bgImg{ 
	position: absolute;
	top: 0;
	left: 0;
	border: 0;
	z-index: 1;
    width: 100%;
    height: 100%;
}

#bgVideo{ 
	position: absolute;
	top: 0;
	left: 0;
	border: 0;
	z-index: 2;
    width: 100%;
    height: 100%;
}
		
#bodyDummy{ 
	position: absolute;
	top: 0;
	left: 0;
	z-index: 3;
	overflow: auto;
	width: 100%;
	height: 100%;
}

Hope I could help. Let me know when you could find a solution that the video does not maintain the aspect ratio, so it could fill the whole browser window so we do not have to put a bgimage.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRolandView Question on Stackoverflow
Solution 1 - VideosydlawrenceView Answer on Stackoverflow
Solution 2 - VideoJey BalachandranView Answer on Stackoverflow
Solution 3 - VideomoritzView Answer on Stackoverflow