我使用iframe
<iframe title="YouTube" src="http://www.youtube.com/embed/aaabbbcccddd?wmode=transparent&autoplay=0" allowfullscreen="allowfullscreen"></iframe>
我希望如果用户点击“播放”按钮,它会自动全屏显示 与用户相反,用户应点击“播放”并点击“全屏”按钮 - 我想跳过点击“全屏”部分。
这可能吗?
我是否需要使用Youtube的Javascript API?
是否有类似这样的内容:
youtube.onPlayButtonClicked(function() {
youtube.fullscreen();
});
提前谢谢!
答案 0 :(得分:4)
使用youtube API即可。
var player, iframe;
var $ = document.querySelector.bind(document);
// init player
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '200',
width: '300',
videoId: 'dQw4w9WgXcQ',
events: {
'onReady': onPlayerReady
}
});
}
// when ready, wait for clicks
function onPlayerReady(event) {
var player = event.target;
iframe = $('#player');
setupListener();
}
function setupListener (){
$('button').addEventListener('click', playFullscreen);
}
function playFullscreen (){
player.playVideo();//won't work on mobile
var requestFullScreen = iframe.requestFullScreen || iframe.mozRequestFullScreen || iframe.webkitRequestFullScreen;
if (requestFullScreen) {
requestFullScreen.bind(iframe)();
}
}
你可以在这个链接上有一个例子: https://codepen.io/bfred-it/pen/GgOvLM