<div class="video-container swiper-slide">
<iframe id="my-iframe" class="iframe" src="https://www.youtube.com/embed/rR8XP5ZfaFk?controls=0&autoplay=1&mute=1&playsinline=1&loop=1&playlist=rR8XP5ZfaFk"></iframe>
<!-- iframe id="myIframe" src="https://player.vimeo.com/video/848389404?background=1&mute=1&loop=1&autoplay=1&autopause=0" allow=autoplay></iframe -->
</div>
<!-- a href="https://alvarotrigo.com/blog/how-to-create-a-youtube-video-background-with-css/" target="_blank" class="read-article">
Read the article 👉
</a -->
<a href="#" class="toggle">
toggle 👉
</a>
*, ::before, ::after {
box-sizing: border-box;
box-sizing: border-box;
}
body{
overflow-x: hidden;
margin: 0;
}
.video-container{
background-color: #222;
position: relative;
width: 100;
/* height = 100 * (9 / 16) = 56.25 */
height: 56.25vw;
overflow: hidden;
}
/* jura */
/* embed-responsive-1084/488 */
/*
.embed-responsive-1084\/488 {
height: 45.01vw;
}
*/
.video-container iframe {
position: absolute;
top: 50%;
left: 50%;
width: 100vw;
height: 100%;
transform: translate(-50%, -50%);
pointer-events: none;
border: none;
}
@media (min-aspect-ratio: 16/9) {
.video-container iframe {
/* height = 100 * (9 / 16) = 56.25 */
height: 56.25vw;
}
}
@media (max-aspect-ratio: 16/9) {
.video-container iframe {
/* width = 100 / (9 / 16) = 177.777777 */
width: 177.78vh;
}
}
/* jura */
@media (min-aspect-ratio: 1084/488) {
.video-container iframe {
height: 45.01vw;
}
}
@media (max-aspect-ratio: 1084/488) {
.video-container iframe {
width: 222.17vh;
}
}
/* DO NOT COPY. NOT PART OF THE EXAMPLE */
.toggle,
.read-article{
position: absolute;
top: 10px;
left: 10px;
z-index: 999;
color: #000;
background: white;
padding: 10px 20px;
font-family: arial;
text-decoration: none;
}
.start {
left: 120px;
}
.read-article:hover{
background: #d5d5d5;
box-shadow: rgb(50 50 93 / 25%) 0 0 100px -20px, rgb(0 0 0 / 30%) 0 0 60px 0px;
}
iframe[sandbox] .read-article{
display: none;
}
/*
const iframe = document.getElementById('my-iframe');
if (iframe.contentWindow.document.readyState === 'complete') {
console.log('Iframe is loaded and has content');
} else {
console.log('Iframe is not yet loaded or has no content');
}
*/
const toggleButton = document.querySelector('.toggle')
toggleButton.addEventListener('click', () => {
toggleVideo()
})
function toggleVideo() {
const slides = document.querySelectorAll(".swiper-slide")
slides.forEach((slide) => {
if (slide.querySelector('iframe')) {
let iframe = slide.querySelector('iframe')
let iframeSrc = iframe.src;
if (iframeSrc.includes('autoplay=1')) {
iframe.src = iframeSrc.replace('autoplay=1', 'autoplay=0');
} else if (iframeSrc.includes('autoplay=0')) {
iframe.src = iframeSrc.replace('autoplay=0', 'autoplay=1');
}
}
})
}
/* in swiper
on: {
slideChange: function () {
toggleVideo()
}
}
*/
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.