gpt4 book ai didi

javascript - screen.lockOrientation 不是函数

转载 作者:数据小太阳 更新时间:2023-10-29 06:09:59 25 4
gpt4 key购买 nike

我想在 Chrome 中使用 Js 中的 API 屏幕。

 if ( navigator.userAgent.match( /(android|iphone)/gi ) ) {
if ( 'orientation' in screen ) {
//console.log( '// API supported, yeah!' );
//console.log( 'new orientation is ', screen.orientation );
screen.lockOrientation( 'landscape' );
} else {
console.log( '// API not supported ' );
}
} else {
//alert('none');
}

我的错误 js:捕获类型错误:screen.lockOrientation 不是函数

/*其他*/

if ( navigator.userAgent.match( /(android|iphone)/gi ) ) {
if ( 'orientation' in screen ) {
let locOrientation = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation;
locOrientation('landscape');
console.log( '// API supported, yeah!' , locOrientation);
console.log( 'new orientation is ', screen.orientation );
//screen.lockOrientation( 'landscape' );
} else {
console.log( '// API not supported ' );
}
} else {
//alert('none');
}

我的错误 js:locOrientation 不是函数

更新:20/04/2017 -> 使用 Javascript,我们无法使用导航器锁定方向。

最佳答案

这是因为 lockOrientation 仍然是前缀。请参阅 this 中的 [2] 脚注MDN 文章。此外,在 Chrome 中,它位于 orientation.lock 下。请参阅 MDN 文章中的 [1] 脚注。

locOrientation = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation || screen.orientation.lock;
locOrientation('landscape');

请注意,并非所有浏览器都具有此功能。使用前请检查locOrientation是否设置。

编辑将较新的 orientation.lock 添加到示例中。

关于javascript - screen.lockOrientation 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42956350/

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