我正在嘗試製作一個按鈕,可以在特定網站上切換(開啓/關閉)HTML5全屏模式。使用JavaScript切換HTML5 FullScreen API
閱讀大量文檔後,看起來瀏覽器在處理某些屬性時仍然存在一些不一致之處。
我去過一種「跨瀏覽器」的方法,其中在Firefox和Safari/MacOS中工作,部分在Safari/Windows中工作,並完全無法在Chrome和Opera中工作。
一些閹割的代碼片段:
// class init
initialize: function() {
this.elmButtonFullscreen = $('fullscreen');
this.elmButtonFullscreen.on('click', this.onClickFullscreen.bindAsEventListener(this));
},
// helper methods
_launchFullScreen: function(element) {
if(element.requestFullScreen) { element.requestFullScreen(); }
else if(element.mozRequestFullScreen) { element.mozRequestFullScreen(); }
else if(element.webkitRequestFullScreen) { element.webkitRequestFullScreen(); }
},
_cancelFullScreen: function() {
if(document.cancelFullScreen) { document.cancelFullScreen(); }
else if(document.mozCancelFullScreen) { document.mozCancelFullScreen(); }
else if(document.webkitCancelFullScreen) { document.webkitCancelFullScreen(); }
},
_isFullScreen: function() {
fullScreen = document.fullscreenEnabled || document.mozFullscreenEnabled || document.webkitFullscreenEnabled ? true : false;
if(this.debug) console.log('Fullscreen enabled? ' + fullScreen);
return fullScreen;
},
// callbacks
onClickFullscreen: function(e) {
e.stop();
if(this._isFullScreen()) this._cancelFullScreen();
else this._launchFullScreen(document.documentElement);
}
至少在FF24中,它是mozFullScreenEnabled(屏幕大寫s) – Narretz 2013-10-15 09:09:46
也不是,如果瀏覽器能夠顯示,則_isFullScreen會返回全屏,如果全屏當前處於活動狀態則不行。您需要監聽document.fullscreenElement及其瀏覽器變體。 – Narretz 2013-10-15 09:26:56