start/play embedded (iframe) youtube-video on click of an image

前端 未结 7 1284
一整个雨季
一整个雨季 2020-12-07 22:20

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 s

相关标签:
7条回答
  • 2020-12-07 22:46

    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.

    0 讨论(0)
  • 2020-12-07 22:47

    The quick and dirty way is to simply swap out the iframe with one that has autoplay=1 set using jQuery.

    THE HTML

    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 JQUERY

    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>');
    }
    
    0 讨论(0)
  • 2020-12-07 22:47

    Example youtube iframe

    <iframe id="video" src="https://www.youtube.com/embed/xNM7jEHgzg4" frameborder="0"></iframe>
    

    The click to play HTML element

    <div class="videoplay">Play</div> 
    

    jQuery code to play the Video

    $('.videoplay').on('click', function() {
        $("#video")[0].src += "?autoplay=1";
    });
    

    Thanks to https://codepen.io/martinwolf/pen/dyLAC

    0 讨论(0)
  • 2020-12-07 22:48

    To start video

    var videoURL = $('#playerID').prop('src');
    videoURL += "&autoplay=1";
    $('#playerID').prop('src',videoURL);
    

    To stop video

    var videoURL = $('#playerID').prop('src');
    videoURL = videoURL.replace("&autoplay=1", "");
    $('#playerID').prop('src','');
    $('#playerID').prop('src',videoURL);
    

    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

    0 讨论(0)
  • 2020-12-07 22:49

    Html Code:-

    <a href="#" id="playerID">Play</a>
    <iframe src="https://www.youtube.com/embed/videoID" class="embed-responsive-item" data-play="0" id="VdoID" ></iframe>
    

    Jquery Code:-

    $('#playerID').click(function(){
        var videoURL = $('#VdoID').attr('src'),
        dataplay = $('#VdoID').attr('data-play');
    
        //for check autoplay
        //if not set autoplay=1
        if(dataplay == 0 ){
            $('#VdoID').attr('src',videoURL+'?autoplay=1');
            $('#VdoID').attr('data-play',1);
         }
         else{
            var videoURL = $('#VdoID').attr('src');
            videoURL = videoURL.replace("?autoplay=1", "");
            $('#VdoID').prop('src','');
            $('#VdoID').prop('src',videoURL);
    
            $('#VdoID').attr('data-play',0);
         }
    
    });
    

    Thats It!

    0 讨论(0)
  • 2020-12-07 22:51

    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.

    0 讨论(0)
提交回复
热议问题