js 实现浏览器的全屏和退出全屏
对于更多日常应用的公共类的操作方法,能够关注下小滑轮网站 http://www.feiaci.com/#/self/…
/**
* 全屏
*/
let toFullScreen = () => {
let el = document.documentElement;
let rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen;
//typeof rfs != "undefined" && rfs
if (rfs) {rfs.call(el);
} else if (typeof window.ActiveXObject !== "undefined") {
//for IE,这里其实就是模仿了按下键盘的 F11,使浏览器全屏
let wscript = new ActiveXObject("WScript.Shell");
if (wscript != null) {wscript.SendKeys("{F11}");
}
} else {alert("浏览器不反对全屏");
}
}
/**
* 退出全屏
*/
let exitFullscreen = function () {
let el = parent.document;
let cfs = el.cancelFullScreen || el.webkitCancelFullScreen || el.mozCancelFullScreen || el.exitFullScreen;
//typeof cfs != "undefined" && cfs
if (cfs) {cfs.call(el);
} else if (typeof window.ActiveXObject !== "undefined") {
//for IE,这里和 fullScreen 雷同,模仿按下 F11 键退出全屏
let wscript = new ActiveXObject("WScript.Shell");
if (wscript != null) {wscript.SendKeys("{F11}");
}
} else {alert("切换失败, 可尝试 Esc 退出")
}
}