I have a bit of code working which when clicking on an overlay images removes the image and allow it's corresponding video to autoplay. What I am struggling with is how to make this work if there are multiple videos, each with its own overlay image.
Here is a link to the codepen: https://codepen.io/NewbCake/pen/qMMQZo
The problem is that when clicking on any overlay it removes all overlays and plays only the first video. How can I make it work for each one individually? I notice when I change the index of f = $f[0] to f = $f[1] it plays the second video (even clicking on the first video). How do I get it to select the index of the one that it is clicked on?
I am also wondering if there are any foreseeable drawbacks to using this...will work on mobile or will it cause problems?
Thank you in advance for any help!
HTML
<section>
<div class="video-intro">
<div class="image">
<div class="play-button btn"></div>
<a href="javascript:" id="btn_play" class="btn">
<img src="http://placekitten.com/960/540" alt="play video" />
</a>
</div>
<iframe src="https://player.vimeo.com/video/66991893?api=1&title=0&byline=0&portrait=0&color=57c0d4" width="960" height="540" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>
</section>
CSS
section {
display:grid;
grid-template-columns:10vw 500px;
}
/* video intro */
.video-intro {
grid-column: 2 ;
box-shadow:0px 0px 7px #ddd;
margin:0 auto 50px !important;
width:90%;
position:relative;
max-width:960px;
.image {
position:absolute;
top:0;
left:0;
z-index:20;
img {
width:100%;
height:auto;
}
JS
$(function(){
var $parent = $('.video-intro'),
$f = $parent.find('iframe'),
$image = $parent.find('.image'),
f = $f[0],
url = $f.attr('src').split('?')[0];
window.addEventListener('message', function(e){
var d = JSON.parse(e.data);
}, false);
$('.btn').click(function(){
var $t = $(this);
runCommand('play');
$image.hide();
});
function runCommand(cmd){
var data = {method : cmd};
f.contentWindow.postMessage(JSON.stringify(data), url);
}
// fitvids
$('.video-intro').fitVids();
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…