Skip to main content
Use this API to lock the screen to a specific orientation. This is useful for video players, games, or other content that requires a specific orientation.

Parameters

PropertyTypeRequiredDescription
orientationStringYesTarget orientation: auto, portrait, landscape
successFunctionNoCallback on success
failFunctionNoCallback on failure
completeFunctionNoCallback that always executes

Code Example

Basic Usage

my.setScreenOrientation({
  orientation: 'landscape',
  success() {
    console.log('Orientation set to landscape');
  }
});

Video Player Full Screen

Page({
  enterFullscreen() {
    my.setScreenOrientation({
      orientation: 'landscape',
      success: () => {
        this.setData({ isFullscreen: true });
      }
    });
  },

  exitFullscreen() {
    my.setScreenOrientation({
      orientation: 'portrait',
      success: () => {
        this.setData({ isFullscreen: false });
      }
    });
  },

  onUnload() {
    // Ensure orientation is reset
    my.setScreenOrientation({ orientation: 'auto' });
  }
});

Game Orientation Lock

Page({
  onLoad() {
    // Lock to landscape for game
    my.setScreenOrientation({
      orientation: 'landscape'
    });
  },

  onUnload() {
    // Allow auto rotation when leaving
    my.setScreenOrientation({
      orientation: 'auto'
    });
  }
});

Orientation Options

ValueDescription
autoAllow automatic rotation based on device orientation
portraitLock to portrait (vertical) mode
landscapeLock to landscape (horizontal) mode
Always reset orientation to auto when leaving a page that locked the orientation, otherwise users may be stuck in an unexpected orientation.

my.getScreenOrientation

Get current orientation

my.onScreenOrientationChange

Listen for changes