haizlin/fe-interview

[js] 第215天 用js实现最大化和最小化窗口

Opened this issue · 4 comments

第215天 用js实现最大化和最小化窗口

我也要出题

const toggleFullScreen = () => {
    if (!document.fullscreen && !document.fullscreenElement) {
        document.documentElement.requestFullscreen();
        // false, null
    } else {
        document.exitFullscreen();
        // true, html dom
    }
}

document.addEventListener("keydown", (e) => {
    // Enter
    if (e.keyCode === 13) {
        toggleFullScreen();
    }
}, false);

https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API

全屏不算吧,最小化真的能实现吗,IE 可能阔以

chrome

利用 requestFullScreen 即可实现浏览器全屏:

document.documentElement.requestFullscreen();

function toFull() {
    document.documentElement.requestFullscreen();
}
<button onclick="toFull()">全屏</button>

退出全屏 exitFullscreen

function exitFull() {
    document.exitFullscreen();
}
<button onclick="exitFull()">退出全屏</button>

全屏:
export const reqFullScreen = () => {
if (document.documentElement.requestFullScreen) {
document.documentElement.requestFullScreen()
} else if (document.documentElement.webkitRequestFullScreen) {
document.documentElement.webkitRequestFullScreen()
} else if (document.documentElement.mozRequestFullScreen) {
document.documentElement.mozRequestFullScreen()
}
}
退出全屏
export const exitFullScreen = () => {
if (document.documentElement.requestFullScreen) {
document.exitFullScreen()
} else if (document.documentElement.webkitRequestFullScreen) {
document.webkitCancelFullScreen()
} else if (document.documentElement.mozRequestFullScreen) {
document.mozCancelFullScreen()
}
}