Overlay youtube embedded video with image, hide image when clicked and autoplay

前端 未结 3 640
独厮守ぢ
独厮守ぢ 2020-12-17 06:31

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 wi

相关标签:
3条回答
  • 2020-12-17 07:04

    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>
    
    0 讨论(0)
  • 2020-12-17 07:14

    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>
    
    0 讨论(0)
  • 2020-12-17 07:21

    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).
    0 讨论(0)
提交回复
热议问题