This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The lockOrientation
method locks the screen into the specified orientation.
Note: This method only works for installed Web apps or for Web pages in full-screen mode.
lockedAllowed = window.screen.lockOrientation(orientation);
The following strings represent the possible orientation requirements you may specify.
portrait-primary
portrait-secondary
landscape-primary
landscape-secondary
portrait
portrait-primary
and portrait-secondary
.landscape
landscape-primary
and landscape-secondary
.default
portrait-primary
and landscape-primary
depends on natural orientation of devices. For example, if the panel resolution is 1280*800, default
will make it landscape, if the resolution is 800*1280, default
will make it to portrait.Note: It's possible to set several locks at the same time. So, if the lock is set for only one orientation, the screen orientation will never change until the screen orientation is unlocked. Otherwise, the screen orientation will change from an orientation to another as long as the orientations are amongst the orientations the device has been locked to.
Returns true
if the orientation was authorized to be locked or false
if the orientation locking was denied. Note that the returns value doesn't indicate that the screen orientation is indeed locked: there may be a delay.
DOMString
argumentscreen.lockOrientationUniversal = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation; if (screen.lockOrientationUniversal("landscape-primary")) { // orientation was locked } else { // orientation lock failed }
Array
argumentscreen.lockOrientationUniversal = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation; if (screen.lockOrientationUniversal(["landscape-primary", "landscape-secondary"])) { // orientation was locked } else { // orientation lock failed }
Specification | Status | Comment |
---|---|---|
Screen Orientation API The definition of 'Screen Orientation' in that specification. | Working Draft | Initial definition |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 38[1] | (Yes) moz[2] | 11 ms[3] | No support | No support |
Array argument | No support | 18.0 (18.0) | 11 ms | No support | No support |
default | No support | No support | No support | No support | No support |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | No support | 38[1] | (Yes) moz[2] | No support | No support | No support |
Array argument | No support | No support | 18.0 (18.0) | No support | No support | No support |
default | No support | No support | 26.0 (26.0) | No support | No support | No support |
[1] A similar method is implemented using a newer standard syntax (screen.orientation.lock
) and returns a Promise
. While it exists on desktop, it will always fail.
[2] This API is only implemented as a prefixed method (screen.mozLockOrientation
) in Firefox OS and Firefox for Android. Also, due to bug 966480, it does not work in Firefox for Android.
[3] This method is implemented using a prefix (screen.msLockOrientation
) in Internet Explorer for Windows 8.1 and Windows RT 8.1. It is not supported on Windows 7.
Screen.orientation
Screen.unlockOrientation()
Screen.onorientationchange
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/screen/lockOrientation