webseite/assets/youtube-embed.html
2019-02-12 17:45:44 +01:00

121 lines
3.4 KiB
HTML

<!-- Defines element markup -->
<template>
<style>
.youtube {
background-color: #000;
margin-bottom: 30px;
position: relative;
padding-top: 56.25%;
overflow: hidden;
cursor: pointer;
}
.youtube img {
width: 100%;
top: -16.82%;
left: 0;
opacity: 0.7;
}
.youtube .play-button {
width: 90px;
height: 60px;
background-color: #333;
box-shadow: 0 0 30px rgba( 0,0,0,0.6 );
z-index: 1;
opacity: 0.8;
border-radius: 6px;
}
.youtube .play-button:before {
content: "";
border-style: solid;
border-width: 15px 0 15px 26.0px;
border-color: transparent transparent transparent #fff;
}
.youtube img,
.youtube .play-button {
cursor: pointer;
}
.youtube img,
.youtube iframe,
.youtube .play-button,
.youtube .play-button:before {
position: absolute;
}
.youtube .play-button,
.youtube .play-button:before {
top: 50%;
left: 50%;
transform: translate3d( -50%, -50%, 0 );
}
.youtube iframe {
height: 100%;
width: 100%;
top: 0;
left: 0;
}
</style>
<div class="youtube">
<div class="play-button"></div>
</div>
</template>
<script>
(function(window, document, undefined) {
// Refers to the "importer", which is index.html
var thatDoc = document;
// Refers to the "importee", which is youtube-embed.html
var thisDoc = (thatDoc._currentScript || thatDoc.currentScript).ownerDocument;
// Gets content from <template>.
var template = thisDoc.querySelector( 'template' ).content;
// Creates an object based in the HTML Element prototype.
var YoutubeProto = Object.create( HTMLElement.prototype );
// Fires when an instance of the element is created.
YoutubeProto.createdCallback = function() {
// Creates the shadow root.
var shadowRoot = this.createShadowRoot();
// Adds a template clone into shadow root.
var clone = thatDoc.importNode( template, true );
shadowRoot.appendChild( clone );
var embed = this.getAttribute( "embed" );
var video = shadowRoot.querySelector( ".youtube" );
this.doTheThing( embed, video );
};
YoutubeProto.doTheThing = function( embedID, videoElem ) {
var source = "https://img.youtube.com/vi/"+ embedID +"/sddefault.jpg";
var image = new Image();
image.src = source;
image.addEventListener( "load", function() {
videoElem.appendChild( image );
});
videoElem.addEventListener( "click", function() {
var iframe = document.createElement( "iframe" );
iframe.setAttribute( "frameborder", "0" );
iframe.setAttribute( "allowfullscreen", "" );
iframe.setAttribute( "src", "https://www.youtube-nocookie.com/embed/"+ embedID +"?rel=0&showinfo=0&autoplay=1" );
this.innerHTML = "";
this.appendChild( iframe );
} );
};
// Registers <youtube-embed> in the main document.
window.youtubeEmbed = thatDoc.registerElement( 'youtube-embed', {
prototype : YoutubeProto
});
})(window, document);
</script>