Make sure it fits you. Use the same button principle:
I imagine that example might satisfy you
I merged the two previous examples:
var buttons = $('ul li');
var videos = $('#videoGallery div');
var titles = $('#videoGallery div h3');
var iframe = $('#videoGallery iframe');
var urls = [
"https://www.youtube.com/embed/6AmRg3p79pM?enablejsapi=1",
"https://www.youtube.com/embed/MkLFlaWxgJA?enablejsapi=1",
"https://www.youtube.com/embed/kIhe7nFcbUg?enablejsapi=1",
"https://www.youtube.com/embed/El3IZFGERbM?enablejsapi=1",
"https://www.youtube.com/embed/6AmRg3p79pM?enablejsapi=1",
"https://www.youtube.com/embed/MkLFlaWxgJA?enablejsapi=1",
"https://www.youtube.com/embed/kIhe7nFcbUg?enablejsapi=1",
"https://www.youtube.com/embed/El3IZFGERbM?enablejsapi=1"
]
buttons.on('click', function (e) {
var index = buttons.get().indexOf(this);
var videoIndex = videos.eq(index);
iframe.attr("src", urls[index])
buttons.removeClass("selected")
$(this).toggleClass("selected")
videos.removeClass('yesDisplay');
videoIndex.toggleClass('yesDisplay');
$('body').animate({scrollTop:(videoIndex.offset().top)}, 200);
});
.video {
display:none;
}
.yesDisplay {
display:block !important;
}
img{
height: 80px
}
ul{
margin: 0;
padding: 0;
}
ul li{
display: block;
height: 30px;
line-height: 30px;
background: #333333;
margin: 0;
cursor: pointer;
padding-left: 10px;
color: #ffffff;
}
ul li:hover{
background: #666666;
color: #ffffff;
}
.selected{
background: #cccccc;
color: #333;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="videoGallery">
<ul>
<li>Vídeo 01</li>
<li>Vídeo 02</li>
<li>Vídeo 03</li>
<li>Vídeo 04</li>
<li>Vídeo 05</li>
<li>Vídeo 06</li>
<li>Vídeo 07</li>
<li>Vídeo 08</li>
</ul>
<div class="yesDisplay video">
<h3>Vídeo 01</h3>
<img src="http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg" alt="">
<p>Este é o vídeo 1:</p>
</div>
<div class="video">
<h3>Vídeo 02</h3>
<img src="http://goo.gl/ytbJn8" alt="">
<p>Este é o vídeo 2:</p>
</div>
<div class="video">
<h3>Vídeo 03</h3>
<img src="http://www.last-video.com/wp-content/uploads/2013/11/superbe-image-de-poissons-sous-l-eau.jpg" alt="">
<p>Este é o vídeo 3:</p>
</div>
<div class="video">
<h3>Vídeo 04</h3>
<img src="http://joombig.com/demo-extensions1/images/gallery_slider/Swan_large.jpg" alt="">
<p>Este é o vídeo 4:</p>
</div>
<div class="video">
<h3>Vídeo 05</h3>
<img src="http://www.conceptcarz.com/images/Citroen/2010-Citroen-Survolt-Concept-Image-01.jpg" alt="">
<p>Este é o vídeo 5:</p>
</div>
<div class="video">
<h3>Vídeo 06</h3>
<img src="http://i1008.photobucket.com/albums/af201/visuallightbox/Butterfly/8-1.jpg" alt="">
<p>Este é o vídeo 6:</p>
</div>
<div class="video">
<h3>Vídeo 07</h3>
<img src="http://michaeldaviddesign.com/themes/escape/files/stacks_image_85.jpg" alt="">
<p>Este é o vídeo 7:</p>
</div>
<div class="video">
<h3>Vídeo 08</h3>
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcS_grGhfAqTFlVrVmKC3HJ9R6CuXPfgz6U6ikgOnfgHxiu38c13" alt="">
<p>Este é o vídeo 8:</p>
</div>
<iframe width="560" height="315" src="https://www.youtube.com/embed/6AmRg3p79pM?controls=0&showinfo=0" frameborder="0" allowfullscreen></iframe>
</div>
You can notice that it will load very quickly, since as I said in the comment the other elements load quickly. And that’s what happens, it does not load all the videos, but od other html elements. There is only one iframe
and that’s who will change your src
by clicking on a button. The others (image and text in my example) will change with the corresponding video.
Other examples
First case
First, to achieve the feat of pausing the youtube video from the iframe
, you must replace the ?controls=0&showinfo=0
for ?enablejsapi=1
at the end of the urls of your videos, as it will be possible to use the api that enables these functions.
With this just select the iframe
desired with the attribute contentWindow
and apply to him the:
.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*')
// pauseVideo - outra opção seria o playVideo
And for the video to appear as at the beginning:
.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*');
However, in the latter case, in addition to ?enablejsapi=1
should be added after &version=3&playerapiid=ytplayer
.
Working example: Jsfiddle
Second case
Remember that this API is unique to youtube. Each host (Vimeo, Uol, Dailymotion, Youtube...) will have its own. As can be observed in this example: https://css-tricks.com/play-button-youtube-and-vimeo-api/.
However, there is a generic way to stop videos regardless of their origins. Simply reset your src
. This will only serve to stop, as has already been said. However, it will require greater performance and will make the appearance of the video not instantaneous.
In the example below the video 01 belongs to Vimeo, already the video 02 belongs to Dailymotion and load normally. For this, in your iframe
you must capture the code for the embed
in the area focused on this in the host’s own site.
Example: Jsfiddle
Third case
I created a array
with the urls of each video. Then I finished the same way we did iframes
by means of the index
of buttons
.
var buttons = $('ul li');
var videos = $('#videoGallery div');
var titles = $('#videoGallery div p');
var iframe = $('#videoGallery div iframe');
var urls = [
"https://www.youtube.com/embed/6AmRg3p79pM?enablejsapi=1",
"https://www.youtube.com/embed/MkLFlaWxgJA?enablejsapi=1",
"https://www.youtube.com/embed/kIhe7nFcbUg?enablejsapi=1",
"https://www.youtube.com/embed/El3IZFGERbM?enablejsapi=1"
]
function pauseVideo() {
var iframes = document.getElementsByTagName("iframe");
iframes[0].contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
}
buttons.on('click', function() {
var index = buttons.get().indexOf(this);
var videoIndex = videos.eq(index);
titles.html("Vídeo " + (parseFloat(index)+1));
iframe.attr("src", urls[index])
$('body').animate({
scrollTop: (videos.offset().top)
}, 200);
buttons.removeClass("selected")
$(this).toggleClass("selected")
pauseVideo();
});
.yesDisplay {
display: block !important;
}
ul {
margin: 0;
padding: 0;
}
ul li {
display: block;
height: 30px;
line-height: 30px;
background: #333333;
margin: 0;
cursor: pointer;
padding-left: 10px;
color: #ffffff;
}
ul li:hover {
background: #666666;
color: #ffffff;
}
.selected {
background: #cccccc;
color: #333;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="videoGallery">
<ul>
<li>Vídeo 01</li>
<li>Vídeo 02</li>
<li>Vídeo 03</li>
<li>Vídeo 04</li>
</ul>
<div class="yesDisplay">
<p>Video 1</p>
<iframe width="560" height="315" src="https://www.youtube.com/embed/6AmRg3p79pM?enablejsapi=1" frameborder="0" id="IframeVideo1" allowfullscreen></iframe>
</div>
</div>
I found this solution very interesting. I see if I can fix the toggle of what I posted. Maybe yours would be cool to put a link next to the title of the video to return.
– Huskell