See if it suits you. Uses the same button principle:
I imagine this example can 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><divid="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 may 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 the other html elements. There is only iframe
and it's the one that will change your src
when clicking any button. The others (image and text, in my example) will change with the corresponding video.
Other examples
First case
First of all, in order to get the pause of youtube video from iframe
, you should replace the ?controls=0&showinfo=0
with ?enablejsapi=1
with the end of your videos urls, so that it will be possible to use api that enables these functions.
With this, just select the iframe
you want with the contentWindow
attribute and apply it to:
.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":""}', '*');
In the latter case, in addition to ?enablejsapi=1
must be added after &version=3&playerapiid=ytplayer
.
Example working: JsFiddle
Second case
Remember that this API is specific to youtube. Each host (Vimeo, Uol, Dailymotion, Youtube ...) will have its own. As can be seen in this example: link .
However, there is a generic way to stop videos regardless of their source. Just reset your src
. This will only serve to stop, as has already been said. However, it will require more performance and will not make the video appear instantaneously.
In the example below video 01 belongs to Vimeo, video 02 belongs to Dailymotion and loads normally. To do this, in your iframe
you should capture the code for the embed
in the area targeted to this on the host's own site.
Example: JsFiddle
Third case
I created a array
with the urls of each video. Then I accessed the same way as in iframes
by means of 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><divid="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>