【发布时间】:2015-10-11 00:22:29
【问题描述】:
我可以在可见性更改为隐藏时暂停视频,并在可见性恢复为可见时播放视频。像这样:
var userManuallyPause = false;
var video = document.getElementById('video');
var documentTitle = document.title;
var updateTitleForVideo = function(state){
if (state === '') {
document.title = documentTitle;
return;
};
document.title = documentTitle + ' [' + state + ']';
};
video.onpause = function(){
userManuallyPause = true;
updateTitleForVideo('Paused');
};
video.onplay = function(){
updateTitleForVideo('');
};
document.addEventListener('visibilitychange', function(){
var state = document.visibilityState;
if (!video.paused) {
if (state === 'hidden') {
video.pause();
userManuallyPause = false;
updateTitleForVideo('Paused');
}
}
else if (state === 'visible' && !userManuallyPause) { video.play(); }
});
但如果视频在可见性变为隐藏之前已经暂停,我不希望视频在我恢复可见时播放。
这可能吗?我不确定。
【问题讨论】: