gpt4 book ai didi

javascript - React 支持 fullscreenchange 事件吗?

转载 作者:行者123 更新时间:2023-12-05 00:35:32 24 4
gpt4 key购买 nike

我在 React 中尝试过 onFullScreenChange 但它不起作用,所以我必须使用 addEventListener 来处理我的情况,我想知道 React 是否支持 fullscreenchange 事件?谢谢你的帮助

最佳答案

没有onFullScreenChange ,您可以在 related docs 中查看所有支持的事件.
您可以查看 useFullscreenStatus 的示例在自定义钩子(Hook)中使用它的实现:

export default function useFullscreenStatus(elRef) {
const [isFullscreen, setIsFullscreen] = React.useState(
document[getBrowserFullscreenElementProp()] != null
);

const setFullscreen = () => {
if (elRef.current == null) return;

elRef.current
.requestFullscreen()
.then(() => {
setIsFullscreen(document[getBrowserFullscreenElementProp()] != null);
})
.catch(() => {
setIsFullscreen(false);
});
};

React.useLayoutEffect(() => {
document.onfullscreenchange = () =>
setIsFullscreen(document[getBrowserFullscreenElementProp()] != null);

return () => (document.onfullscreenchange = undefined);
});

return [isFullscreen, setFullscreen];
}

function getBrowserFullscreenElementProp() {
if (typeof document.fullscreenElement !== "undefined") {
return "fullscreenElement";
} else if (typeof document.mozFullScreenElement !== "undefined") {
return "mozFullScreenElement";
} else if (typeof document.msFullscreenElement !== "undefined") {
return "msFullscreenElement";
} else if (typeof document.webkitFullscreenElement !== "undefined") {
return "webkitFullscreenElement";
} else {
throw new Error("fullscreenElement is not supported by this browser");
}
}

关于javascript - React 支持 fullscreenchange 事件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64485142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com