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.
You are supposed to be able to specify a domain that is safe for scripting. the api document mentions "As an extra security measure, you should also include the origin parameter to the URL" http://code.google.com/apis/youtube/iframe_api_reference.html src="http://www.youtube.com/embed/J---aiyznGQ?enablejsapi=1&origin=mydomain.com" would be the src of your iframe.
however it is not very well documented. I am trying something similar right now.
Html Code:-
Jquery Code:-
Thats It!
The quick and dirty way is to simply swap out the
iframe
with one that hasautoplay=1
set using jQuery.THE HTML
Placeholder:
Autoplay link:
THE JQUERY
The
onClick
catcher that calls the functionThe function
Example youtube iframe
The click to play HTML element
jQuery code to play the Video
Thanks to https://codepen.io/martinwolf/pen/dyLAC
This should work perfect just copy this div code
To start video
To stop video
You may want to replace "&autoplay=1" with "?autoplay=1" incase there are no additional parameters
works for both vimeo and youtube on FF & Chrome