javascript
  • jquery
  • html5
  • youtube
  • magnific-popup
  • 2017-02-13 209 views 0 likes 
    0

    我已經包含在以下DIV YouTube視頻:播放/暫停YouTube視頻中拉網

    <div id="playVideo" class="white-popup mfp-hide"> 
        <div class='embed-container'> 
         <iframe id="video" src='https://www.youtube.com/embed/GYmW1u8YiQY?enablejsapi=1&version=3&playerapiid=ytplayer?rel=0' frameborder='0' allowfullscreen="true" allowscriptaccess="always"></iframe> 
        </div> 
    </div> 
    

    而且按鈕元素綁定到打開它:

    <div class="ghost-button float"> 
         <a href="#playVideo" id="playPop" class="open-popup-link" style="color:black; font-size:24px; letter-spacing:2px; font-family: 'Pathway Gothic One', sans-serif;" onclick="">WATCH TRAILER</a> 
        </div> 
    

    這是一個隱藏彈出使用magnific pop

    我有以下JavaScript播放視頻時用戶單擊以打開彈出:

    $('.open-popup-link').magnificPopup({ 
         type:'inline', 
         midClick: true 
        }); 
    
    $(document).ready(function() { 
         $('#playPop').on('click', function(ev){ 
          $('#video')[0].contentWindow.postMessage('{"event":"command","func":"' + 'playVideo' + '","args":""}', '*'); 
         }); 
    
         $('.closebtn').on('click', function(ev2){ 
          $('#video')[0].contentWindow.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*'); 
         }); 
    
         $('button .mfp-close').on('click', function(ev3){ 
          $('#video')[0].contentWindow.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*'); 
         }); 
    
        }); 
    

    目前,按鈕關閉視頻工作,但只有在用戶手動點擊後才能播放。當他們點擊打開包含它的彈出窗口時,視頻不會自動播放。如何在點擊時自動播放,並在點擊另一個按鈕時停止播放?

    回答

    0

    嘗試在鏈接源中添加屬性autoplay =「true」,如下所示。

    src ='https://www.youtube.com/embed/GYmW1u8YiQY?autoplay=true&enablejsapi=1&version=3&playerapiid=ytplayer?rel=0'

    +0

    這不是你的jQuery代碼的問題。 –

    +0

    在URL中設置自動播放真實屬性的問題是,當我關閉按鈕時,它隱藏了視頻,但該元素仍然存在,基本上重新加載並從頭開始播放。我需要以某種方式暫停或停止它,或者殺死DOM對象並找到重新加載它的方法。試圖避免需要使用YouTube API,如果我能幫助它。 –

    相關問題