javascript - screen.lockOrientation 不是函数

标签 javascript mobile responsive

我想在 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/

相关文章:

java - Web 服务器向 J2ME 应用程序发送命令

javascript - 我怎样才能让我的背景视频全屏 react ?

JavaScript 警报不会触发

javascript - AngularJS ng-template with ng-messages-include

javascript - 如何更改 Jquery require_from_group 方法的默认样式?

iphone - 在 iPhone Mobile Safari 中单指滚动内部内容(div/iframe)

javascript - 为移动设备禁用 jQuery 下拉菜单

html - 使用 Bootstrap 的响应式内联 SVG

javascript - 在保持图像大小的同时在响应调整大小的 Canvas 上绘制

javascript - jQuery 添加和删除类