Last active
March 6, 2018 10:21
-
-
Save xmon/da5bab437143699d984f86bb06ac800e to your computer and use it in GitHub Desktop.
Video youtube, loop y background fullscreen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="es"> | |
<head> | |
<!-- Required meta tags --> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | |
<link rel="stylesheet" href="style.css"> | |
</head> | |
<body> | |
<div class="cover"> | |
<div class="hi"> | |
This is fully responsive & mobile friendly YouTube video background scaled with 16/9 aspect ratio. | |
</div> | |
</div> | |
<div class="tv"> | |
<div class="screen mute" id="tv"></div> | |
</div> | |
<script src="script.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tag = document.createElement('script'); | |
tag.src = 'https://www.youtube.com/player_api'; | |
var firstScriptTag = document.getElementsByTagName('script')[0]; | |
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); | |
var tv, | |
playerDefaults = { | |
autoplay: 0, | |
loop: 1, | |
autohide: 1, | |
modestbranding: 0, | |
rel: 0, | |
showinfo: 0, | |
controls: 0, | |
disablekb: 1, | |
enablejsapi: 0, | |
iv_load_policy: 3 | |
}; | |
var vid = [ | |
{'videoId': '_f1jPbN_2ng', 'startSeconds': 10, 'endSeconds': 40, 'suggestedQuality': 'hd720'}, | |
]; | |
function onYouTubePlayerAPIReady() { | |
tv = new YT.Player('tv', { | |
events: {'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange}, | |
playerVars: playerDefaults | |
}); | |
} | |
function onPlayerReady() { | |
console.log('onPlayerReady'); | |
tv.loadVideoById(vid[0]); | |
tv.mute(); | |
$('#tv').addClass('active'); | |
} | |
function onPlayerStateChange(e) { | |
if (e.data === 0) { | |
tv.seekTo(vid[0].startSeconds); | |
} | |
} | |
function vidRescale() { | |
var w = $(window).width() + 200, | |
h = $(window).height() + 200, | |
screen = $('.tv .screen'); | |
if (w / h > 16 / 9) { | |
tv.setSize(w, w / 16 * 9); | |
screen.css({'left': '0px'}); | |
} else { | |
tv.setSize(h / 9 * 16, h); | |
screen.css({'left': -(screen.outerWidth() - w) / 2}); | |
} | |
} | |
$(window).on('load resize', function () { | |
vidRescale(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.cover { | |
position: absolute; | |
top: 0; | |
left: 0; | |
z-index: 2; | |
width: 100%; | |
height: 100%; | |
.hi { | |
position: absolute; | |
top: 50%; | |
left: 50%; | |
transform: translate(-50%,-50%); | |
color: #fff; | |
font-family: 'Roboto Slab', serif; | |
font-size: 24px; | |
line-height: 26px; | |
text-align: center; | |
span { | |
color: #ff0; | |
cursor: pointer; | |
text-decoration: underline; | |
} | |
em { | |
font-style: normal; | |
&.hidden { | |
display: none; | |
} | |
} | |
} | |
} | |
.tv { | |
position: absolute; | |
top: 0; | |
left: 0; | |
z-index: 1; | |
width: 100%; | |
height: 100%; | |
overflow: hidden; | |
.screen { | |
position: absolute; | |
top: 0; | |
right: 0; | |
bottom: 0; | |
left: 0; | |
z-index: 1; | |
margin: auto; | |
opacity: 0; | |
transition: opacity .5s; | |
&.active { | |
opacity: 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment