方案一
//js
switchFullScreen() {
const docElm = document.documentElement;
if (!this.isFullScreen) {
if (docElm.requestFullscreen) {
docElm.requestFullscreen();
} else if ((docElm).webkitRequestFullScreen) {
(docElm).webkitRequestFullScreen();
}
this.isFullScreen = true;
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if ((document).webkitCancelFullScreen) {
(document).webkitCancelFullScreen();
}
this.isFullScreen = false;
}
}
//ts
switchFullScreen(): void {
const docElm = document.documentElement;
if (!this.isFullScreen) {
if (docElm.requestFullscreen) {
docElm.requestFullscreen();
} else if ((docElm as any).webkitRequestFullScreen) {
(docElm as any).webkitRequestFullScreen();
}
this.isFullScreen = true;
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if ((document as any).webkitCancelFullScreen) {
(document as any).webkitCancelFullScreen();
}
this.isFullScreen = false;
}
}
方案二
function exitFullScreen() {
$('#data_screen').data('open', false);
$('#data_screen .item_title').text('全屏')
};
function enterFullScreen() {
initCont();
$('#data_screen').data('open', true);
$('#data_screen .item_title').text('退出')
};
// 全屏
$('#data_screen .item_title').on('click', () => {
if ($('#data_screen').data('open')) {
// 退出全屏
// exitFullScreen();
var cfs = document.cancelFullScreen || document.webkitCancelFullScreen || document.mozCancelFullScreen || document.exitFullScreen;
if (cfs) {
cfs.call(document);
} else if (typeof window.ActiveXObject !== "undefined") {
var wscript = new ActiveXObject("WScript.Shell");
if (wscript != null) {
wscript.SendKeys("{F11}");
}
}
} else {
// 进入全屏
// enterFullScreen()
var docEle = document.documentElement;
var rfs = docEle.requestFullScreen || docEle.webkitRequestFullScreen || docEle.mozRequestFullScreen || docEle.msRequestFullScreen;
if (rfs) {
rfs.call(docEle);
} else if (typeof window.ActiveXObject !== "undefined") {
var wscript = new ActiveXObject("WScript.Shell");
if (wscript != null) {
wscript.SendKeys("{F11}");
}
}
}
});
// 监听全屏事件触发
function fullscreenchange() {
let isFullScreen = document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen;
if (isFullScreen) {
// 进入全屏
enterFullScreen()
} else {
// 退出全屏
exitFullScreen();
}
}
['fullscreenchange', 'webkitfullscreenchange', 'mozfullscreenchange'].forEach((item, index) => {
window.addEventListener(item, () => fullscreenchange());
})