Lock Device Screen Orientation with JavaScript
The window
object has a great API for working with screens(mobile devices, etc) and their related metadata - window.screen
and window.screen.orientation
.
For Mobile Devices and Full Screen browsers, you can use the following methods to toggle orientation locks:
window.screen.orientation.lock("portrait-primary")
window.screen.orientation.unlock()
Note that when you call the lock API on a web browser that is not full screen, it will raise a DOMException
similar to following:
DOMException: screen.orientation.lock() is not available on this device.
Acceptable orientation values can be found in the docs linked below
https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation/lock
Tweet