<section>
<header>
<h1>Tabbed Video Module</h1>
<p>For a client project, they wanted a tabbed video player. The basics of it were pretty easy (and can be accomplished a number of ways) but I had a <em>very</em> hard time getting the videos to stop when I clicked on the next tab. Google eventually revealed the secret was to remove the SRC from the other IFRAMEs on click. Works for both YouTube and Vimeo embeds.</p>
<p>Hope you enjoy these videos courtesy of <a href="http://www.prettyboidrag.com" target="_blank">Pretty Boi Drag</a>!</p>
</header>
<div class="container">
<ul class="tabs">
<li>
<h2>First Video</h2>
<iframe src="https://player.vimeo.com/video/406701198" width="640" height="360" frameborder="0" allow="autoplay; fullscreen" allowfullscreen></iframe>
</li>
<li>
<h2>Second Video</h2>
<iframe src="https://player.vimeo.com/video/399275772" width="640" height="360" frameborder="0" allow="autoplay; fullscreen" allowfullscreen></iframe>
</li>
<li>
<h2>Third Video</h2>
<iframe src="https://player.vimeo.com/video/388008030" width="640" height="360" frameborder="0" allow="autoplay; fullscreen" allowfullscreen></iframe>
</li>
</ul>
</div>
</section>
*,
*:before,
*:after {
box-sizing: border-box;
}
body {
background-color: lightgray;
line-height: 1.4;
}
header {
padding: 30px;
color: white;
width: 100%;
background-color: black;
flex: 35%;
margin-bottom: 5%;
p {
max-width: 50%;
font-size: 18px;
a {
color: yellow;
text-decoration: none;
}
}
}
section {
min-height: 100vh;
display: flex;
flex-direction: column;
.container {
width: 100%;
max-width: 80%;
margin: 0 auto;
flex: 60%;
display: flex;
justify-content: center;
align-items: center;
padding: 80px 0;
ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
width: 100%;
position: relative;
padding: 0 0 56.25%;
li {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 0;
padding: 0 0 56.25%;
h2 {
background-color: black;
color: white;
position: absolute;
height: 80px;
top: -80px;
display: flex;
justify-content: center;
align-items: center;
margin: 0;
width: calc(100% / 3);
border-left: 1px solid lightgray;
font-size: 18px;
transition: background-color 200ms ease-in;
}
&:first-child {
h2 {
border-left: none;
}
}
&.active {
z-index: 10;
h2 {
background-color: blue;
}
}
iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
}
}
}
}
View Compiled
// Title/Tab
var title = $("ul.tabs li h2");
// When Browser Resizes (or Loads) Recalculate Everything
$(window).on("resize load", function () {
// Width of the UL
var ulWidth = $("ul.tabs").outerWidth();
// How many LIs are in the UL
var liCount = $("ul.tabs li").length;
// UL width devided by how many LIs equals the width of each LI
var liWidth = ulWidth / liCount;
//For each H2 add margin-left x its index. This will stagger them appropriately.
title.each(function (i) {
$(this).css({
marginLeft: liWidth * i
});
});
});
// Add the class active to just the first LI
$("ul.tabs li").first().addClass("active");
// Find the SRC of each video and add it as data-url on each LI. This becomes important since we will have to remove the SRC on each IFRAME. A simple show/hide will not stop the video from playing when you click the next tab. You have to remove the URL all together. Unfortunately, this also means each video will start completely over when you click on the tab.
$("ul.tabs li").each(function (i, video) {
var video_url = $(this).find("iframe").attr("src");
$(this).attr("data-url", video_url);
});
// Remove the SRC from each IFRAME contained in each LI. See above for explanation.
$("ul.tabs li").not(":first-child").find("iframe").attr("src", "");
// Clicking on a title first removes the class active from all other LIs, then adds it to the one you just clicked on. Next, the SRC is removed for all of the other videos and last, the URL from data-url is then added to the SRC for the video in the current LI. (Did that make sense?)
title.on("click", function () {
var video_url = $(this).parent().attr("data-url");
$("ul.tabs li").removeClass("active");
$(this).parent().addClass("active");
$("ul.tabs li").find("iframe").attr("src", "");
$(this).parent().find("iframe").attr("src", video_url);
});
This Pen doesn't use any external CSS resources.