0
我試過一些解決方案,但沒有成功。Youtube Iframe API僅適用於第一個
Youtube視頻是隱藏的div內。
點擊按鈕和視頻出現,但是當我在暫停按鈕點擊,在div必要再隱藏(強制性和工作)
在第二次的時候,我點擊播放視頻按鈕,視頻顯示,但沒有自動播放暫停都不會觸發隱藏事件。
爲什麼?
的jsfiddle:https://jsfiddle.net/u19ut5gt/6/
HTML
<div id="fw16-liam">
<div class="fw16-button playvideo">Play Video</div>
<div id="liam-video"><div id="ytplayer"></div></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
的Javascript
$("#fw16-liam .playvideo").click(function(){
$("#liam-video").fadeIn(1000);
runPlayer('0m9hYRUV920');
});
if(document.getElementById('iframe_api') === null){
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
tag.id = "iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
function runPlayer(video_id){
var player;
player = new YT.Player('ytplayer', {
playerVars: {
autoplay: 1,
html5: 1,
controls: 1,
showsearch: 0,
showinfo: 0,
loop: 1,
fs: 0
},
height: '300',
width: '487',
videoId: video_id,
events: {
onStateChange: function (event) {
// Loop after video end
if(event.data === YT.PlayerState.ENDED){
event.target.playVideo();
}
// If user, pause video, then this hide.
if(event.data == 2){
event.target.stopVideo();
$("#liam-video").fadeOut(200);
}
}
}
});
}
CSS
.playvideo{width:112px; height: 37px; color: white;}
.fw16-button {cursor: pointer;cursor: hand; background-color: red;}
#liam-video{display:none; background: #9e7969; width: 487px; height:677px; position: relative; left: 0px; top: 20px; border: none;}
#liam-video iframe{border:none;}
就是這樣!謝謝。它完美地工作 –