我有一个奇怪的问题。我有一台LG g700平板电脑,我想设置方向锁定。但是当我在我的nexus 5上测试它时,它可以很好地使用以下代码:
window.screen.orientation.lock("landscape")
但是当我在平板电脑上执行相同的代码时,它无法正常工作,并且会出现如下错误:
Uncaught (in promise) DOMException: The page needs to be fullscreen in order to call lockOrientation(). {message: "The page needs to be fullscreen in order to call lockOrientation().", name: "SecurityError", code: 18, INDEX_SIZE_ERR: 1, DOMSTRING_SIZE_ERR: 2…}code: 18message: "The page needs to be fullscreen in order to call lockOrientation()."name: "SecurityError"__proto__: DOMException
有解决方案吗?
答案 0 :(得分:1)
这只能在全屏模式下完成。因此,我建议您在致电screen.orientation
之前添加超时,以确保全屏处于活动状态并准备就绪。
if (document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen) {
setTimeout(function(){
window.screen.orientation.lock("landscape");
} , 200);
}