screen.orientation

Javascript

画面の向きを取得する

console.log( screen.orientation.type );

--->
portrait-primary:デバイスの自然な縦向き
portrait-secondary:デザインの上下逆さまな状態の縦向き
landscape-primary:デバイスの自然な横向き
landscape-secondary:デバイスの上下逆さまな状態の横向き

画面の向きが変わったら処理を実行

window.addEventListener( "orientationchange", function(){

  video = document.querySelector("#video");
  
  if ( screen.orientation.type === "landscape-secondary" ) {
    console.log("画面が横向きになりました");
    video.classList.remove(...element.classList); // 一旦すべてのクラスを消去する
    video.classList.add("landscape");
  } 
});

画面の向きによるデザインをCSSで変更

@media screen and (orientation: landscape) {
  body {
    transform: rotate(90deg);
  }
}
@media screen and (orientation: portrait) {
  body {
    transform: rotate(0deg);
  }
}

画面の向きをロックする

screen.orientation.lock("landscape");
BACK