screen.lockOrientation is not a function

旧城冷巷雨未停 提交于 2021-02-16 16:29:08

问题


I would like to use the API screen in Js with chrome.

 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');
    }

my error js : caught TypeError: screen.lockOrientation is not a function

/* other */

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');
}

my error js : locOrientation is not a function

update : 20/04/2017 -> With Javascript, we can't lock orientation with the navigator.


回答1:


This is because lockOrientation is still prefixed. See the [2] footnote in this MDN article. Also, in Chrome it is under orientation.lock. See the [1] footnote in the MDN article.

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

Please note that not all browsers have this function. Please check if locOrientation is set before you use it.

Edit: Adding the newer orientation.lock to the example.



来源:https://stackoverflow.com/questions/42956350/screen-lockorientation-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!