uniapp 锁定竖屏,固定竖屏,锁定屏幕
锁定竖屏:
// 强制竖屏if (typeof plus !== 'undefined') {// 可选值:// 'portrait':竖屏(允许上下翻转)// 'portrait-primary':仅正竖屏(禁止倒转)plus.screen.lockOrientation('portrait');// uni.showToast({ title: '已锁定竖屏', icon: 'none' });} else {// uni.showToast({ title: '仅支持 App 端', icon: 'none' });}
所有案例:
<template><view class="container"><button @click="lockPortrait" class="btn">锁定竖屏</button><button @click="lockLandscape" class="btn">锁定横屏</button><button @click="unlockOrientation" class="btn">解除锁定</button></view>
</template><script>
export default {data() {return {};},onReady() {// 页面就绪后,先锁定为竖屏(可选)this.lockPortrait();},methods: {// 锁定竖屏lockPortrait() {// 确保在 App 环境下调用(H5/小程序不支持)if (typeof plus !== 'undefined') {// 可选值:// 'portrait':竖屏(允许上下翻转)// 'portrait-primary':仅正竖屏(禁止倒转)plus.screen.lockOrientation('portrait');uni.showToast({ title: '已锁定竖屏', icon: 'none' });} else {uni.showToast({ title: '仅支持 App 端', icon: 'none' });}},// 锁定横屏lockLandscape() {if (typeof plus !== 'undefined') {// 可选值:// 'landscape':横屏(允许左右翻转)// 'landscape-primary':仅正横屏(如Home键在右侧)plus.screen.lockOrientation('landscape');uni.showToast({ title: '已锁定横屏', icon: 'none' });} else {uni.showToast({ title: '仅支持 App 端', icon: 'none' });}},// 解除锁定(恢复系统自动旋转)unlockOrientation() {if (typeof plus !== 'undefined') {plus.screen.unlockOrientation();uni.showToast({ title: '已解除锁定', icon: 'none' });}}}
};
</script><style scoped>
.container {display: flex;flex-direction: column;gap: 30rpx;padding: 50rpx;
}.btn {padding: 20rpx;background-color: #3c9cff;color: white;border-radius: 8rpx;
}
</style>