Only load YouTube iframes when actually clicked on.

This commit is contained in:
Kaj Forney 2023-02-23 12:35:26 -07:00
parent 1cec7ae75e
commit 5aad2cb576
Signed by: kforney
GPG key ID: 3AB4E2E04CEF656F
2 changed files with 102 additions and 36 deletions

View file

@ -149,14 +149,6 @@ hr {
margin-right: auto;
}
iframe[src*=youtube] {
text-align: center;
display: block;
margin-right: auto;
margin-left: auto;
max-width: 80%;
}
iframe[src*=itch] {
text-align: center;
display: block;
@ -261,22 +253,67 @@ iframe[src*=itch] {
text-shadow: #78a1bb 3px 3px 3px;
}
.youtube-container {
/*Code for YouTube CSS is originally from https://stackoverflow.com/questions/50410828/how-to-prevent-youtube-js-calls-from-slowing-down-page-load
and was then modified by me as necessary.*/
.youtube-video {
background-color: #000;
margin-bottom: 30px;
position: relative;
padding-top: 56.25%;
overflow: hidden;
width: 100%;
padding-top: 50.25%; /* 16:9 Aspect Ratio (divide 9 by 16 = 0.5625) */
cursor: pointer;
border-radius: 40px;
}
.youtube-iframe {
.youtube-video img {
width: 100%;
top: 0%;
left: 0;
opacity: 0.7;
}
.youtube-video .playButton {
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-video .playButton:before {
content: "";
border-style: solid;
border-width: 15px 0 15px 26.0px;
border-color: transparent transparent transparent #fff;
}
.youtube-video img,
.youtube-video .playButton {
cursor: pointer;
}
.youtube-video img,
.youtube-video iframe,
.youtube-video .playButton,
.youtube-video .playButton:before {
position: absolute;
}
.youtube-video .playButton,
.youtube-video .playButton:before {
top: 50%;
left: 50%;
transform: translate3d(-50%, -50%, 0);
}
.youtube-video iframe {
height: 100%;
width: 100%;
top: 0;
left: 0;
bottom: 0;
right: 0;
width: 100%;
height: 100%;
border-radius: 40px;
}
.fa-brands {

View file

@ -4,7 +4,7 @@
<head>
<meta charset="utf-8">
<title>Kaj Forney -- Portfolio</title>
<meta name="description" content="">
<meta name="description" content="Portfolio website of Kaj Forney.">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="manifest" href="site.webmanifest">
@ -116,11 +116,10 @@
<div class="mt-4"></div>
<div class="row">
<div class="youtube-container col">
<iframe class="youtube-iframe" src="https://www.youtube.com/embed/N5unzGlC9ME"
title="YouTube video player"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen loading="lazy"></iframe>
<div class="col">
<div class="youtube-video" data-embed="N5unzGlC9ME">
<div class="playButton"></div>
</div>
</div>
</div>
@ -195,12 +194,15 @@
<div class="mt-5"></div>
<div class="row">
<div class="col youtube-container">
<iframe class="youtube-iframe" width="560" height="315"
<div class="col">
<div class="youtube-video" data-embed="nra31mia8Y0">
<div class="playButton"></div>
</div>
<!--<iframe class="youtube-iframe" width="560" height="315"
src="https://www.youtube-nocookie.com/embed/nra31mia8Y0" title="YouTube video player"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
allowfullscreen loading="lazy"></iframe>
allowfullscreen loading="lazy"></iframe>-->
</div>
</div>
</div>
@ -329,19 +331,13 @@
<div class="row mt-5">
<div class="col-sm">
<div class="youtube-container">
<iframe class="youtube-iframe" src="https://www.youtube.com/embed/LaloFVeUvOk"
title="YouTube video player"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen loading="lazy"></iframe>
<div class="youtube-video" data-embed="LaloFVeUvOk">
<div class="playButton"></div>
</div>
</div>
<div class="col-sm">
<div class="youtube-container">
<iframe class="youtube-iframe" src="https://www.youtube.com/embed/ahQPl9FOX7A"
title="YouTube video player"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen loading="lazy"></iframe>
<div class="youtube-video" data-embed="ahQPl9FOX7A">
<div class="playButton"></div>
</div>
</div>
</div>
@ -435,9 +431,42 @@
crossorigin="anonymous" defer></script>
<script>
//For setting footer text with current copyright year.
document.getElementById("footerText").innerHTML = "Copyright © Kaj Forney 2018-" + new Date(Date.now()).getFullYear();
</script>
<script>
//This script is for improving page performance by not loading YouTube iFrames until actually necessary.
//Code is originally from https://stackoverflow.com/questions/50410828/how-to-prevent-youtube-js-calls-from-slowing-down-page-load
//and was then modified by me as necessary.
let youtube = document.querySelectorAll( ".youtube-video" );
for (let i = 0; i < youtube.length; i++) {
let source = "https://img.youtube.com/vi/"+ youtube[i].dataset.embed +"/maxresdefault.jpg";
let image = new Image();
image.src = source;
image.addEventListener( "load", function() {
youtube[ i ].appendChild( image );
}( i ) );
youtube[i].addEventListener( "click", function() {
let iframe = document.createElement( "iframe" );
iframe.setAttribute( "frameborder", "0" );
iframe.setAttribute( "allowfullscreen", "" );
iframe.setAttribute( "allow", "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture");
iframe.setAttribute( "src", "https://www.youtube-nocookie.com/embed/"+ this.dataset.embed +"?rel=0&showinfo=0&autoplay=1" );
this.innerHTML = "";
this.appendChild( iframe );
} );
}
</script>
</body>
</html>