最簡單實用的一種辦法:
var element=document.getElementById("fullScreen");
element.style.width="100%";
// 判斷各種瀏覽器肤舞,找到正確的方法
var requestMethod =element.requestFullScreen || //W3C
? ? element.webkitRequestFullScreen || //Chrome等
? ? element.mozRequestFullScreen || //FireFox
? ? element.msRequestFullscreen; //IE11
if (requestMethod) {
requestMethod.call(element);
}
else if (typeof window.ActiveXObject !=="undefined") {//for Internet Explorer
? ? var wscript =new ActiveXObject("WScript.Shell");
if (wscript !==null) {
wscript.SendKeys("{F11}");
}
}?
監(jiān)聽退出全屏的事件(默認(rèn)退出全屏的方法是ESC退出鍵):
if (window.addEventListener) {
document.addEventListener('fullscreenchange',function(){
var isFullscreen = document.fullscreenEnabled ||
window.fullScreen ||
document.mozFullscreenEnabled ||
document.webkitIsFullScreen;
if(!isFullscreen){
var element=document.getElementById("fourView");
element.style.width="62%";
//執(zhí)行的方法
}
});
document.addEventListener('webkitfullscreenchange',function(){
var isFullscreen = document.fullscreenEnabled ||
window.fullScreen ||
document.mozFullscreenEnabled ||
document.webkitIsFullScreen;
if(!isFullscreen){
//執(zhí)行的方法
}
});
document.addEventListener('mozfullscreenchange',function(){
var isFullscreen = document.fullscreenEnabled ||
window.fullScreen ||
document.mozFullscreenEnabled ||
document.webkitIsFullScreen;
if(!isFullscreen){
//執(zhí)行的方法
}
});
document.addEventListener('MSFullscreenChange',function(){
var isFullscreen = document.fullscreenEnabled ||
window.fullScreen ||
document.mozFullscreenEnabled ||
document.webkitIsFullScreen;
if(!isFullscreen){
//執(zhí)行的方法
}
});
}
判斷當(dāng)前是否有全屏的元素:
var isFullscreen = document.fullscreenEnabled ||
window.fullScreen ||
document.mozFullscreenEnabled ||
document.webkitIsFullScreen ||
document.msFullscreenElement;
值得注意的是,IE11的情況比較特殊楚里,它是只能返回當(dāng)前被全屏的元素砚偶,而不能判斷當(dāng)前是否有元素全屏造虎,所以我們只能通過判斷是否有元素的方式來確認(rèn)是否全屏巡莹。
退出全屏的方法:
var el = document;
me.isFullScreen=false;
var cfs =el.cancelFullScreen ||el.webkitCancelFullScreen ||
el.mozCancelFullScreen ||el.exitFullScreen ||el.msExitFullscreen;
if (cfs) {//typeof cfs != "undefined" && cfs
? ? cfs.call(el);
}else if (typeof window.ActiveXObject !="undefined") {
//for IE兵迅,這里和fullScreen相同泰讽,模擬按下F11鍵退出全屏
? ? var wscript =new ActiveXObject("WScript.Shell");
if (wscript !=null) {
wscript.SendKeys("{F11}");
}
};
目前關(guān)于網(wǎng)上很多設(shè)置IE11的方法都是錯誤的例衍,需要特別注意。有些是大小寫的問題已卸,有些是方法名更改過了佛玄,這里直接貼出微軟的api文檔,方便大家查閱咬最。
https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/dev-guides/dn265028(v=vs.85)