Hi Sitepoint folks,
I’m trying to add video thumbnail links to my portfolio. I’ve gotten one to work where it loads the video, plays, and then when it closes it removes the src. It’s great. It took me about a day to get the right combo of video commands, but I’ve arrived.
Now I’m trying to replicate the same set of commands with video #2 and I realize that my video commands hinge their reference of getElementByID(“video1”). So the caveman in me first thinks repeat all of this 4 times (for the 4 videos, e.g. video1, video2, video3, video4), but that’s a terribly redundant use of code.
I can’t do get ElementsByClassName because then it’s liable to open all the videos, so I’m stuck with a “this” instance question of how do I script in JS to say, “Hey, reference this element in question that I’m clicking on”.
Does that question make sense? If anybody can point me in the direction of how “this” is used in syntax for this sort of thing, I’d really appreciate it. Thank for reading.
HTML:
<!-- MOTION EXAMPLES -->
<div id="m1" class="thumb25 hide">
<a data-fancybox data-src="#v1" class="fancybox">
<div id="v1" style="display:none">
<video id='myVideo' width="600" >
<source src="__section_01_portfolio/_motion/v1.mp4" type="video/mp4">
Your browser does not support HTML5 videos.
</video>
</div>
<div class="thumbM">
<img id="gif1" class="imageM" src="_img/_thumbs/m_01.jpg" />
</div>
<ul class="text_thumb">
<li class="caption">AMD | HP</li>
<li class="sub-caption">ANIMATION (VIDEO)</li>
</ul>
</a>
</div>
<div id="m2" class="thumb25 hide">
<a data-fancybox data-src="#v2" class="fancybox">
<div id="v2" style="display:none">
<video id='myVideo2' width="600" >
<source src="__section_01_portfolio/_motion/v2.mp4" type="video/mp4">
Your browser does not support HTML5 videos.
</video>
</div>
<div class="thumbM">
<img id="gif2" class="imageM" src="_img/_thumbs/m_02.jpg" />
</div>
<ul class="text_thumb">
<li class="caption">AMD | HP</li>
<li class="sub-caption">ANIMATION (VIDEO)</li>
</ul>
</a>
</div>
JS:
$(document).ready(function() {
$("[data-fancybox]").fancybox({
afterShow: function() {
var vid = document.getElementById("myVideo2");
vid.play();
},
afterClose: function () {
var vid = document.getElementById("myVideo2");
vid.pause();
vid.currentTime = 0;
vid.removeAttribute('src'); // empty source
}
});
});