Created
January 11, 2021 10:48
-
-
Save eduard-un/ff0e9335e7cf299f70cf3bf396852af1 to your computer and use it in GitHub Desktop.
Js code for preloading Youtube Videos in Divi
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() { | |
| setTimeout( function() { | |
| var youtube = document.querySelectorAll( ".youtube" ); | |
| for ( var i = 0; i < youtube.length; i++ ) { | |
| var source = "https://img.youtube.com/vi/" + youtube[ i ].dataset.embed + "/sddefault.jpg"; | |
| var image = new Image(); | |
| image.src = source; | |
| image.addEventListener( "load", function() { | |
| youtube[ i ].appendChild( image ); | |
| }( i ) ); | |
| youtube[ i ].addEventListener( "click", function() { | |
| var iframe = document.createElement( "iframe" ); | |
| iframe.setAttribute( "frameborder", "0" ); | |
| iframe.setAttribute( "allowfullscreen", "" ); | |
| iframe.setAttribute( "src", "https://www.youtube.com/embed/" + this.dataset.embed + "?rel=0&showinfo=0&autoplay=1" ); | |
| this.innerHTML = ""; | |
| this.appendChild( iframe ); | |
| } ); | |
| }; | |
| }, 100 ); | |
| } )(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment