screen.lockOrientation不是函数

Seb*_*Seb 5 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');
    }
Run Code Online (Sandbox Code Playgroud)

我的错误js:捕获了TypeError: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');
}
Run Code Online (Sandbox Code Playgroud)

我的错误js:locOrientation不是函数

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

Jos*_*els 6

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

locOrientation = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation || screen.orientation.lock;
locOrientation('landscape');
Run Code Online (Sandbox Code Playgroud)

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

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