JS实现页面横竖屏切换事件监听

43 阅读1分钟

Screen学习资料

window.addEventListener("orientationchange", function () {
  console.log(screen.orientation.type + " " + 
  
  screen.orientation.angle
  );
});

screen.orientation.addEventListener(
  "change",
  function (e) {
    console.log(screen.orientation.type + " " + screen.orientation.angle);
  },
  false
);

screen.orientation.type

  1. portrait-primary:竖屏状态并且旋转角度为 0 度,也就是设备的自然位置
  2. landscape-primary:横屏状态并且旋转角度为 90 度
  3. portrait-secondary:竖屏状并且即旋转角度为 180 度,也就是倒着拿的位置
  4. landscape-secondary:横屏状态并且旋转角度为 270 度