2

I would like to display an image before viewing a youtube video in my page. Is there a way to do this with jquery or a javascript function.

I want to overlay this with my own image and when clicked, display the video and autoplay it, all wihtout reloading the page.

Thank you for your help

Regards Judi

brightmist.co.uk
  • 531
  • 5
  • 18
  • 47

3 Answers3

4

If using the HTML5 iframe from YouTube, the above won't work as the video will play in the background with autoplay=1 set BEFORE the image is clicked. I instead used .append() to append the iframe after the click on the image so the video would only autoplay after the click event for the image.

<body>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script> 

<div id="ytapiplayer2" style="display:none;">
</div>

<img src="https://i.qmyimage.com/mgen/global/zGetImageNew.ms?args=%22shpimg4198.jpg%22,425,96,1" id="imageID" />
<script type="text/javascript">
$('#imageID').click(function() {
$('#ytapiplayer2').show();
$('#ytapiplayer2').append('<iframe width="1130" height="636" src="http://www.youtube.com/embed/YOURVIDEOCODEHERE?autoplay=1" frameborder="0" allowfullscreen></iframe>');
$('#imageID').hide();
});
</script>
</body>
Mike Averto
  • 655
  • 1
  • 10
  • 16
  • Wow thanks Mike this is much better however I've used the iframe in the video div instead as that's more user friendly when putting in lots of videos – brightmist.co.uk Mar 31 '13 at 19:56
4

I am sorry the last solution to your issue was incorrect, well no it wasn't but too complicated for what you wanted.

youtube videos will not play if hidden. so add your embed code and set the autoplay=1 but hide the div and then do the jquery flip on the elements. if you copy the code below and run it it should work (except you have to point it to your jquery library)

<body>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script> 

<div id="ytapiplayer2" style="display:none;">
<object width="1280" height="745">
<param name="movie" value="http://www.youtube.com/v/kCfP003Btjw?fs=1&hl=en_US&rel=0&autoplay=1"></param>
<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/kCfP003Btjw?fs=1&hl=en_US&rel=0&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="1280" height="745"></embed>
</object>

</div>


<img src="https://i.qmyimage.com/mgen/global/zGetImageNew.ms?args=%22shpimg4198.jpg%22,425,96,1" id="imageID" />
<script type="text/javascript">
$('#imageID').click(function() {
$('#ytapiplayer2').show();
$('#imageID').hide();
});
</script>
</body>
BrandonS
  • 932
  • 7
  • 16
3

As this could be done pretty easy with the javascript api I have also created this effect with the New iFrame Embedding mode (which i find is pretty sweet). It's really simple

  1. Show your image
  2. When clicked, replace it with your iframe embed code (with the "autoplay=1" parameter).
ullmark
  • 2,469
  • 1
  • 19
  • 28