2016-06-25 59 views
0

jQuery的:如何在更換視頻時停止視頻(Jquery)?

$(function() { 

$('.videos').hide(); 
$(".bat").click(function() { 
    $('.videos').hide(); 
    $('.morcego').slideDown(); 
    $('.default').replaceWith(".morcego"); 
}); 
$('.hit').click(function() { 
    $('.videos').hide(); 
    $('.careca').slideDown(); 
    $('.default').replaceWith(".careca"); 
}); 
$('.metro').click(function() { 
    $('.videos').hide(); 
    $('.metro-last').slideDown(); 
    $('.default').replaceWith(".metro-last"); 
    }); 
}); 

HTML:` 觀看和收聽我們的評論

 <iframe width="900" height="400" src="https://www.youtube.com/embed/tl39XPeb7UE?rel=0&amp;showinfo=0" frameborder="0" allowfullscreen class="videos morcego img-responsive"></iframe> 

     <iframe width="900" height="400" src="https://www.youtube.com/embed/g7uYfhWNvrA?rel=0&amp;showinfo=0" frameborder="0" allowfullscreen class="videos careca img-responsive"></iframe> 

     <iframe width="900" height="400" src="https://www.youtube.com/embed/FDIjP6X9f98?rel=0&amp;showinfo=0" frameborder="0" allowfullscreen class="videos metro-last img-responsive">></iframe> 

    </div> 

    <div class="videos-list-video"> 
     <h3 class="align-center">Popular Videos</h3> 
     <div class=img-list> 

      <img src="img/batman_videos.jpg" class="img-responsive bat"> 
     </div> 
     <div class=img-list> 

      <img src="img/hitman_videos.jpg" class="img-responsive hit"> 
     </div> 
     <div class=img-list> 

      <img src="img/metro_videos.png" class="img-responsive metro"> 
     </div> 


    </div> 

</div> 

程序工作正常,但視頻被更換時,另外一個也不會停止播放和你聽到了。我想我不能使用replaceWith()方法...任何建議?由於

回答

0

添加&enablejsapi=1到iframe src屬性

<iframe width="900" height="400" 
    src="https://www.youtube.com/embed/tl39XPeb7UE?rel=0&amp;showinfo=0&enablejsapi=1" 
    frameborder="0" allowfullscreen class="videos morcego img-responsive"></iframe> 

,並呼籲

jQuery("iframe")[0].contentWindow.postMessage('{"event":"command", 
"func":"stopVideo","args":""}', '*'); 

停止iframe中的視頻,您可以使用"func":"pauseVideo"暫停視頻

檢查演示

的jsfiddle:Demo

+0

它的作品,但只適用於蝙蝠俠視頻。如果蝙蝠正在玩並且你打電話給其他人,蝙蝠會停止,但是如果殺手正在玩並且你打電話給其他人,它不會停止。同樣是地鐵...我不明白... – icenine

+0

檢查更新jsfiddle [鏈接](https://jsfiddle.net/h2a8qfh4/1/) – zakhefron

+0

完美!謝謝! – icenine

1

之前隱藏.video,刪除iframe中src

$(this).find('.videos').attr('src', ''); 
+0

你說這個? $(函數(){ $()隱藏() '的視頻。; 「蝙蝠」。 $()點擊(函數(){ $(' 視頻)隱藏() ; $(本).find ATTR( 'src '中, ''); $(' 視頻')。 '了slideDown(); $( 'morcego)。(' 默認')replaceWith(」 .morcego「); }); }); 它不工作... – icenine

+0

@icenine爲了節省時間,你可以把你的工作代碼放在[Plunker.co](http://plnkr.co/)? – zer00ne