2017-07-25 103 views
0

我有我的網站上的YouTube視頻:暫停使用YouTube視頻和顯示DIV與窗體

<div id="video"> 
 
    <iframe width="560" height="315" src="https://www.youtube.com/embed/some_video" frameborder="0" allowfullscreen></iframe> 
 
</div>

一些固定的時間後,我要暫停,並詢問用戶問題通過在視頻覆蓋的一種形式:

<h1>how do you like the video so far? </h1> 
 
<div id="question"> 
 
    <form> 
 
    <input type="radio" name="question" value="poor" checked>Poor<br> 
 
    <input type="radio" name="question" value="good">Good<br> 
 
    <input type="submit" value="Submit"> 
 
    </form> 
 
</div>

什麼是JavaScript代碼來暫停視頻後一段固定的時間,什麼是css用於很好地顯示錶單?基本上我想模仿一次看起來像coursera.org的講座。

+1

[如何在隱藏iframe時暫停YouTube播放器?](https://stackoverflow.com/questions/8667882/how-to-pause-a-youtube-player-when-hiding-the-if​​rame)至於一般停止 – Luca

+0

@LucaKiebel這是不是很重複 – William

+0

基於YT播放器API https://jsfiddle.net/g4b3ghLp/ – marzelin

回答

0

酪氨酸這樣的:

HTML:

<div id="video"> 
      <iframe id="iframe" width="560" height="315" src="https://www.youtube.com/embed/W5MZevEH5Ns" frameborder="0" allowfullscreen ></iframe> 
     </div> 
    <div id="message"> 
     <h1>how do you like the video so far? </h1> 
     <div id="question"> 
      <form> 
      <input type="radio" name="question" value="poor" checked>Poor<br> 
      <input type="radio" name="question" value="good">Good<br> 
      <input type="submit" value="Submit"> 
      </form> 
     </div> 
    <div> 

JQuery的:

<script> 

$(document).ready(function(){ 
     // hiding message on load 
     $("#message").hide(); 

     // time out function 
     setTimeout(function(){ 
     var yourIframe = ('iframe#iframe'); 
     var src = $('#iframe').attr('src');  

     $('#iframe').attr('src', '').attr('src', src);//pause youtube video 
     $("#message").show(); // show message 
     }, 5000); 

}); 

</script> 
+0

這不適合我。無論視頻是否播放,它只是顯示了表單。此外,播放視頻時,代碼不會暫停播放。我還對一些css/javascript組合感興趣,它會在視頻暫停時覆蓋視頻頂部的問題。 –

0

使用下面的代碼暫停和恢復對開放視頻和關閉彈出窗口,而不是停止和啓動視頻再次

var iframe = document.getElementsByTagName("iframe")[0].contentWindow; 

iframe.postMessage('{"event":"command","func": "playVideo","args":""}','*'); 
iframe.postMessage('{"event":"command","func": "pauseVideo","args":""}','*'); 
+0

好吧,這應該暫停視頻,但我該如何讓視頻暫停自己,例如'00:15'?這意味着在視頻的第15秒,所以如果我從第16秒開始觀看,它就不會停下來 –