查看: 24|回复: 0

uniapp APP和微信小程序横屏模式实现代码

[复制链接]

0

主题

0

回帖

0

积分

积极分子

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2008-11-11
发表于 2026-1-12 08:51:13 | 显示全部楼层 |阅读模式

首先在pages.json下新增代码:

"globalStyle": {
		"navigationBarTextStyle": "black",
		"navigationBarTitleText": "uni-app",
		"navigationBarBackgroundColor": "#F8F8F8",
		"backgroundColor": "#F8F8F8",
		"pageOrientation": "auto" //横屏配置 auto自动 | portrait竖屏 | landscape横屏
	},

微信小程序横屏:

pages.json下添加代码(进入页面自动横横屏):

{
			"path": "pages/native/demo",
			"style": {
				"navigationBarTitleText": "横屏模式",
				"enablePullDownRefresh": false,
				"backgroundTextStyle": "dark",
				"mp-weixin": {
					"pageOrientation": "landscape" //auto:自动,landscape:横屏,portrait :竖屏
				}
			}
		}

页面代码实现:

<template>
	<view class="">
		<button type="default" @click="onCrosswise('default')">默认横竖屏切换</button>
		<button type="default" @click="onCrosswise('portrait')">竖屏</button>
		<button type="default" @click="onCrosswise('landscape')">横屏</button>
	</view>
</template>

<script setup>
	import {
		ref,
		reactive,
		watch,
		onMounted,
	} from "vue"
	import {
		onLoad,
		onReady,
		onUnload
	} from '@dcloudio/uni-app'

// 切换模式
	const onCrosswise = (mode) => {
		if (mode == 'default') {
			// #ifdef APP-PLUS
			plus.screen.lockOrientation('default'); // 默认横竖屏切换
			// #endif
		} else if (mode == 'portrait') {
			// #ifdef APP-PLUS
			plus.screen.lockOrientation('portrait-primary'); // 竖屏展示
			// #endif
		} else {
			uni.navigateTo({
				url: "/pages/native/demo",
				success: function(res) {
					// #ifdef APP-PLUS
					plus.screen.lockOrientation('landscape-primary'); // 强制横屏
					// #endif
				}
			})
		}
	}
</script>

/pages/native/demo页面:

<template>
	<view class="">
		跳转成功
	</view>
</template>

<script setup>
	import {
		onLoad,
		onReady,
		onUnload
	} from '@dcloudio/uni-app'
	//页面卸载时切换为默认或者其他属性
	onUnload(() => {
		{
			// #ifdef APP-PLUS
			plus.screen.lockOrientation('default');
			// #endif
		}
	})
</script>

<style>
</style>

总结 

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部