I'm trying to start playing an embedded youtube video by clicking an image. The idea is to have an image on top of a video, and when the image is clicked it fades out and starts playing the video.
I'm using jquery to fade the image, and was hoping to find a way to play or click the video using jquery as well.
The fading is working fine, but I can't figure out how to trigger the video to play. I got it to work on a couple of browsers by setting the video to autoplay and hide it, and then fade in the video when the image was clicked. On most browsers the video would autoplay when faded in, but in chrome it started to autoplay even when it was hidden. It didn't work well in iOS either.
Since I'm pretty new at this, I'm not even sure if I'm writing it 100% correct, but I've tried something like this without success:
$('#IMAGE').click(function() { $('#VIDEO').play(); });
So, how would I go about to make the video play on an image click? Is there a way to play the video when the image is clicked, just using jquery?
Thank you in advance.
Allowing iframes to autoplay video content You should also note the need to use the allowfullscreen attribute in complement to the allow attribute in order to support browsers that do not support allow attribute. If you are using amp-iframe and autoplay you need to add allow="autoplay" to your amp-iframe element.
To have an embedded YouTube video begin playing at a specific timestamp, first calculate the start point in total number of seconds (60 times the number of minutes plus the number of seconds), then do the same for the end point. Grab the embed code from YouTube, by clicking on Share > Embed.
You'll need to be using YouTube on a computer to get the embed code for your video. Go to the video you want to embed. Search for the video, and then click its name in the search results. Decide where you want the embedded video to begin.
The quick and dirty way is to simply swap out the iframe
with one that has autoplay=1
set using jQuery.
Placeholder:
<div id="videoContainer"> <iframe width="450" height="283" src="https://www.youtube.com/embed/VIDEO_ID_HERE?wmode=transparent" frameborder="0" allowfullscreen wmode="Opaque"></iframe> </div>
Autoplay link:
<a class="introVid" href="#video">Watch the video</a></p>
The onClick
catcher that calls the function
jQuery('a.introVid').click(function(){ autoPlayVideo('VIDEO_ID_HERE','450','283'); });
The function
/*-------------------------------- Swap video with autoplay video ---------------------------------*/ function autoPlayVideo(vcode, width, height){ "use strict"; $("#videoContainer").html('<iframe width="'+width+'" height="'+height+'" src="https://www.youtube.com/embed/'+vcode+'?autoplay=1&loop=1&rel=0&wmode=transparent" frameborder="0" allowfullscreen wmode="Opaque"></iframe>'); }
You can do this simply like this
$('#image_id').click(function() { $("#some_id iframe").attr('src', $("#some_id iframe", parent).attr('src') + '?autoplay=1'); });
where image_id is your image id you are clicking and some_id is id of div in which iframe is also you can use iframe id directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With