【vue hooks】useScreenOrientation-获取屏幕方向并支持低版本系统
作者:互联网
2026-04-05
代码
新建一个useScreenOrientation.js文件,代码如下
import { shallowRef } from "vue";
import { useEventListener, useSupported } from "@vueuse/core";
// TypeScript dropped the inline types for these types in 5.2
// We vendor them here to avoid the dependency
// export type OrientationType = 'portrait-primary' | 'portrait-secondary' | 'landscape-primary' | 'landscape-secondary'
// export type OrientationLockType = 'any' | 'natural' | 'landscape' | 'portrait' | 'portrait-primary' | 'portrait-secondary' | 'landscape-primary' | 'landscape-secondary'
// export interface ScreenOrientation extends EventTarget {
// lock: (orientation: OrientationLockType) => Promise
// unlock: () => void
// readonly type: OrientationType
// readonly angle: number
// addEventListener: (type: 'change', listener: (this: this, ev: Event) => any, useCapture?: boolean) => void
// }
// export interface UseScreenOrientationReturn extends Supportable {
// orientation: ShallowRef
// angle: ShallowRef
// lockOrientation: (type: OrientationLockType) => Promise
// unlockOrientation: () => void
// }
/**
* Reactive screen orientation
*
* @see
*
* @__NO_SIDE_EFFECTS__
*/
export function useScreenOrientation(options = {}) {
const isSupported = useSupported(
() => window && "screen" in window && "orientation" in window.screen
);
const screenOrientation = isSupported.value ? window.screen.orientation : {};
const orientation = shallowRef(screenOrientation.type);
const angle = shallowRef(screenOrientation.angle || 0);
const isIOS = /iphone|ipad|ipod/.test(
navigator.userAgent.toLocaleLowerCase()
);
const getLandscape = () => {
if (isIOS && Object.prototype.hasOwnProperty.call(window, "orientation")) {
return Math.abs(window.orientation) === 90;
}
return window.innerHeight / window.innerWidth < 1;
};
if (isSupported.value) {
// 这部分是原代码
useEventListener(
window,
"orientationchange",
() => {
orientation.value = screenOrientation.type;
angle.value = screenOrientation.angle;
},
{ passive: true }
);
} else {
// 新增兼容低版本
const landscapeChange = () => {
orientation.value = getLandscape()
? "landscape-primary"
: "portrait-primary";
};
landscapeChange();
useEventListener(
window,
"orientationchange",
() => {
landscapeChange();
},
{ passive: true }
);
}
const lockOrientation = type => {
if (isSupported.value && typeof screenOrientation.lock === "function")
return screenOrientation.lock(type);
return Promise.reject(new Error("Not supported"));
};
const unlockOrientation = () => {
if (isSupported.value && typeof screenOrientation.unlock === "function")
screenOrientation.unlock();
};
return {
isSupported,
orientation,
angle,
lockOrientation,
unlockOrientation
};
}
相关推荐
专题
+ 收藏
+ 收藏
+ 收藏
+ 收藏
+ 收藏
+ 收藏
最新数据
相关文章
sfsDb 时序数据处理指南
04/13
NineData 成功通过国家高新技术企业认定!
04/13
腾讯云轻量应用服务器管理:自动化轻量服务器 - Openclaw Skills
04/13
系统资源监控器:实时服务器健康追踪 - Openclaw Skills
04/13
前端老兵AI学习过程
04/13
工作笔记-CodeBuddy应用探索
04/13
OpenCode 完全指南:从 0 到 100K Star 的开源 AI 编码 Agent
04/13
Day11-龙虾哥打工日记:OpenClaw救援机器人 - 主系统挂了谁来救场?
04/13
Claude Code 创始人 Boris 揭秘:团队 10 倍效率技巧
04/13
拒绝“手搓”工具!带你硬核手写 MCP Server,解锁 Agent 的无限潜能
04/13
AI精选
