-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvidbg.js
47 lines (36 loc) · 1.2 KB
/
vidbg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* @file vidbg is a 0.3kb micro-library for embedding video backgrounds into web pages.
* Scaffolded with generator-microjs v0.0.12
* @author Daniel Lamb <[email protected]>
*/
function vidbg(element, sources, overlay) {
var container, item, html = [], index = sources.length;
// check if the device supports HTML5 video
if (!document.createElement('video').canPlayType) {
// not supported
return;
}
// start video tag
html.push('<video autoplay="true" loop="loop" muted="muted" volume="0">');
// order matters in some browsers
sources.reverse();
// build video tag source(s) html
while (index--) {
item = sources[index];
html.push('<source src="', item.src, '" type="video/', item.type, '" />');
}
// end video tag
html.push('</video>');
// check for optional overlay element
if (overlay) {
// append optional overlay element
html.push('<div class="vidbg-overlay"></div>');
}
// create video DOM element
container = document.createElement('div');
container.setAttribute('class', 'vidbg');
// set the container HTML
container.innerHTML = html.join('');
// append container to the provided element
element.appendChild(container);
}