47 lines
1.0 KiB
JavaScript
47 lines
1.0 KiB
JavaScript
// useScreenOrientation.ts
|
|
import { onUnmounted } from 'vue'
|
|
|
|
//orientation:'portrait' | 'landscape'
|
|
export function useScreenOrientation() {
|
|
// 锁定屏幕方向
|
|
const lockOrientation = orientation => {
|
|
try {
|
|
if (typeof plus !== 'undefined' && plus.screen) {
|
|
const currentOrientation = plus.screen.orientation
|
|
if (currentOrientation !== orientation) {
|
|
plus.screen.lockOrientation(orientation)
|
|
return true
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.warn('锁定方向失败:', e)
|
|
return false
|
|
}
|
|
return false
|
|
}
|
|
|
|
// 解锁屏幕方向
|
|
const unlockOrientation = () => {
|
|
try {
|
|
if (typeof plus !== 'undefined' && plus.screen) {
|
|
plus.screen.unlockOrientation()
|
|
return true
|
|
}
|
|
} catch (e) {
|
|
console.warn('解锁方向失败:', e)
|
|
return false
|
|
}
|
|
return false
|
|
}
|
|
|
|
// 自动在组件卸载时解锁
|
|
onUnmounted(() => {
|
|
unlockOrientation()
|
|
})
|
|
|
|
return {
|
|
lockOrientation,
|
|
unlockOrientation
|
|
}
|
|
}
|