在youtube iframe嵌入游戏中发起一个事件

Rob*_*ris 11 javascript jquery youtube-api

我需要在youtube iframe上播放一个事件.因此,当按下播放按钮时,我需要调用一个函数来隐藏span.module-strip

我试过这个但也许我走错了路?

$("#home-latest-vid")[0].onplay = function() {
    alert('hi');
};
Run Code Online (Sandbox Code Playgroud)

HTML:

<div class="module module-home-video">

    <span class="module-strip">Latest Product Video</span>

        <iframe id="video" src="http://www.youtube.com/embed/RWeFOe2Cs4k?hd=1&rel=0&autohide=1&showinfo=0&enablejsapi=1" frameborder="0" width="640" height="360"></iframe>
</div>
Run Code Online (Sandbox Code Playgroud)

Luc*_*ofi 21

<script src="https://www.youtube.com/iframe_api"></script>

<div class="module module-home-video">
    <span class="module-strip">Latest Product Video</span>
    <div id="video"></div>
</div>

<script>
    var player, playing = false;
    function onYouTubeIframeAPIReady() {
        player = new YT.Player('video', {
            height: '360',
            width: '640',
            videoId: 'RWeFOe2Cs4k',
            events: {
                'onStateChange': onPlayerStateChange
            }
        });
    }

    function onPlayerStateChange(event) {

      if (event.data == YT.PlayerState.PLAYING) {
         alert('video started');
         playing = true;
        }

      else if(event.data == YT.PlayerState.PAUSED){
            alert('video paused');
            playing = false;
       }
}
</script>
Run Code Online (Sandbox Code Playgroud)

  • 很棒的东西,vimeo iframe 嵌入的过程会类似吗? (2认同)