的問題是,你沒有在iframe中事件的控制,所以當用戶刷過該區域不能告訴。我建議的解決方法是在不觀看的情況下用圖像佔位符替換iframe。爲了做到這一點,請使用YouTube's Iframe API來跟蹤視頻事件。當視頻從播放停止時,我們將隱藏視頻並顯示圖像。 Here is a demo。
HTML
<div ng-app="app">
<div ng-controller="ctrl" ng-swipe-right="msg($event, 'right')" ng-swipe-left="msg($event, 'left')">
<div id="player"></div>
<img id="player-cover" src="http://img.youtube.com/vi/M7lc1UVf-VE/hqdefault.jpg" />
</div>
</div>
JS
在啓動它隱藏的視頻。當圖像被點擊時,它顯示並播放視頻。當視頻從暫停播放onPlayerStateChange
處理切換圖像和視頻。每次在圖像上調用滑動事件時,它也會觸發圖像的單擊事件處理程序。變量swiping
會記錄事件是否只是點擊或滑動。
var app = angular.module('app', ['ngTouch']);
app.controller('ctrl', function($scope) {
$scope.msg = function(event, msg) {
swiping = true;
alert(msg);
}
});
// keep track of the user swiping. onYouTubeIframeAPIReady needs to occur outside of Angular.
var swiping = false;
// Youtube related.
document.getElementById('player').style.display = 'none';
document.getElementById('player-cover').addEventListener("click", function() {
if (swiping) {
swiping = false;
return;
}
document.getElementById('player').style.display = 'block';
player.playVideo();
});
// This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// This function creates an <iframe> (and YouTube player) after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: { 'onStateChange': onPlayerStateChange }
});
}
function onPlayerStateChange(event) {
if (event.data === YT.PlayerState.PAUSED || event.data === YT.PlayerState.ENDED) {
document.getElementById('player-cover').style.display = 'block';
document.getElementById('player').style.display = 'none';
} else {
document.getElementById('player-cover').style.display = 'none';
document.getElementById('player').display = 'block';
}
}
這似乎是同一個問題 - http://stackoverflow.com/questions/28180672/youtube-cannot-swipe-past-iframe-in-carousel-slider - 他們推薦使用代替圖像的iframe的開始。這是一個可行的解決方案嗎? – jjbskir
@jjbskir,嗯,我可以使用自定義播放按鈕的圖像,並用ng-show單擊替換視頻和圖像,但如果用戶暫停視頻,他將無法再次滑動。另外我不知道是否可以用自定義按鈕啓動視頻;) – akn