Based on How to "Lazy Load" Embedded YouTube Videos by Thoriq Firdaus and modified to just replace all Youtube iframes.
Last active
November 10, 2017 20:34
-
-
Save r15ch13/12b453cbfd36fb42267c3d61604ca0de to your computer and use it in GitHub Desktop.
Lazy Load Embedded YouTube Videos
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
| .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; | |
| } |
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
| (function() { | |
| let iframes = [].slice.call(document.getElementsByTagName('iframe')); | |
| let youtube = iframes.filter(function(el){ | |
| return el.src.indexOf('youtube.com/embed') !== -1; | |
| }); | |
| youtube.forEach(function(el) { | |
| let div = document.createElement('div'); | |
| div.setAttribute('class', 'youtube'); | |
| let playButton = document.createElement('div'); | |
| playButton.setAttribute('class', 'play-button'); | |
| div.appendChild(playButton); | |
| let image = new Image(); | |
| let id = el.src.match(/embed\/(.*)$/).slice(-1).pop(); | |
| if(!id) { return; } | |
| image.src = 'https://i.ytimg.com/vi/' + id + '/sddefault.jpg'; | |
| image.addEventListener('load', function() { | |
| div.appendChild(image); | |
| }.bind(this, div)); | |
| div.addEventListener('click', function() { | |
| let iframe = document.createElement('iframe'); | |
| iframe.setAttribute('frameborder', '0'); | |
| iframe.setAttribute('allowfullscreen', 'allowfullscreen'); | |
| iframe.setAttribute('src', 'https://www.youtube.com/embed/' + id + '?rel=0&showinfo=0&autoplay=1'); | |
| this.innerHTML = ''; | |
| this.appendChild(iframe); | |
| }); | |
| el.parentNode.appendChild(div); | |
| el.parentNode.removeChild(el); | |
| }); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment