Youtube视频结束时隐藏iframe

时间:2014-09-25 10:36:08

标签: javascript iframe youtube

提前谢谢大家。 我需要一些帮助隐藏一个iframe youtube视频,当它结束时,有人可以指导我如何做到吗??

 <iframe id="promo_video" src="http://www.youtube.com/embed/XzZudNOwkU8" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen="" style="display:none;position:absolute;top:228px;left:492px;" height="527" width="934" frameborder="0"></iframe>

我需要这样的东西,但我无法找到如何制作它

 <script >
function myFunction()
{
    if (promo_video.end=positive)
    reload anotherother page


    }


</script>

1 个答案:

答案 0 :(得分:2)

希望此解决方案适合您。解决方案的唯一变化是您只需要来自iframe的视频ID。请参考下面提到的小提琴链接。

    <div id="player"></div>
<script src="http://www.youtube.com/player_api"></script>
<script>

    // create youtube player
    var player;
    function onYouTubePlayerAPIReady() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
          videoId: 'XzZudNOwkU8', <!-- video id should be here. last component from video URL -->
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
    }

    // autoplay video
    function onPlayerReady(event) {
        event.target.playVideo();
    }

    // when video ends
    function onPlayerStateChange(event) {        
        if(event.data === 0) {            
            $('#player').css('display', 'none');
        }
    }

</script>

Fiddle link