← Plugins / Screen Orientation
This Apache Cordova plugin lets you lock the screen to a specific orientation, release it back
to the device's automatic rotation, and read the current orientation. It builds on the standard
screen.orientation Web API, so the code looks familiar.
cordova plugin add cordova-plugin-screen-orientation
The plugin extends the global screen.orientation object. Use it after deviceready:
| Member | Description |
|---|---|
screen.orientation.lock(orientation) | Lock to an orientation; returns a Promise. |
screen.orientation.unlock() | Release the lock and allow automatic rotation. |
screen.orientation.type | The current orientation string (read-only). |
screen.orientation.angle | The current rotation angle in degrees (read-only). |
screen.orientation.onchange | Handler fired when the orientation changes. |
Pass one of these strings to lock(): portrait-primary,
portrait-secondary, landscape-primary, landscape-secondary,
portrait, landscape or any.
Force landscape for a video screen, then restore rotation when leaving:
function enterPlayer() {
screen.orientation.lock("landscape");
screen.orientation.addEventListener("change", function () {
console.log("Now: " + screen.orientation.type);
});
}
function leavePlayer() {
screen.orientation.unlock();
}
unlock() when leaving a locked screen, or the rest of your app stays stuck.screen.orientation.type to adapt layout rather than guessing from window size.