当前位置:网站首页>Two horizontal and vertical screen switching schemes for uniapp mobile terminal
Two horizontal and vertical screen switching schemes for uniapp mobile terminal
2022-07-28 07:18:00 【HousLin】
Global configuration horizontal and vertical screens
1、 stay pages.json Add configuration in :
"globalStyle": {
"pageOrientation": "auto"
}
This method can configure the horizontal and vertical screen switching of all pages ()
2、 open uniapp Of manifest.json file , Find the last column of the menu on the left “ Source view ”, Click in , Add the following code at the bottom
"orientation" : [
// Vertical screen positive direction
"portrait-primary",
// The vertical screen is in the opposite direction
"portrait-secondary",
// Horizontal screen positive direction
"landscape-primary",
// The horizontal screen is in the opposite direction
"landscape-secondary",
// Natural direction
"default"
]
Scheme 1 、 Page automatically switched between horizontal and vertical screens
explain : This scheme is to specify a certain page According to the user's mobile phone / Vertical automatic switching , But you need to write two sets of styles , One set horizontally and one set vertically , stay data Define a value to judge horizontal and vertical , Match again uniapp Page lifecycle onResize Listen for changes in the window , Transverse time isLandScape The initial value is true, Vertical false, To the outermost layer view The class style of the root tag uses ternary judgment to dynamically bind ,isLandScape The value of is true Use horizontal screen style ,false Use vertical screen style .1、 Let's take a look at the rendering first

2、 Pages that need automatic switching between horizontal and vertical screens are written
// After the page is loaded, give it to the natural direction , It can automatically switch according to the user's vertical and horizontal
onLoad() {
// #ifdef APP-PLUS
plus.screen.lockOrientation('default');
// #endif
},
// Clear the positive direction of the horizontal screen when the page is closed
onUnload() {
// #ifdef APP-PLUS
plus.screen.lockOrientation('portrait-primary');
// #endif
},
3、 Other pages that don't need horizontal screen for example :index.vue
Set the vertical screen in two lifecycles And clear when closing the page
onLoad() {
// #ifdef APP-PLUS
plus.screen.lockOrientation('portrait-primary');
// #endif
},
onUnload() {
// #ifdef APP-PLUS
plus.screen.lockOrientation('portrait-primary');
// #endif
},
In particular :// #ifdef APP-PLUS And // #endif Is to let the code in app take effect , solve plus is not defined The error of
4、 Core logic code
①、 stay data The initial value to judge the horizontal and vertical screen switching
isLandScape: false // Horizontal screen or not , The default is vertical
②、 In the page lifecycle onResize Changes in the monitoring window in
onResize() {
let _this = this
uni.getSystemInfo({
success: function(res) {
if (res.windowWidth > res.windowHeight) {
// Horizontal screen
_this.isLandScape = true
} else {
// Vertical screen
_this.isLandScape = false
}
}
})
}
5、 Use CSS The preprocessing language writes two sets of styles ( cross / vertical ), I use it scss
// Vertical screen chart_portrait Is the parent wrapper ,tbcls Just my table style , You can write corresponding styles according to your specific needs .chart_portrait {
height: 36vh;
font-size: 2vh;
text-align: center; .tbcls {
width: 97vw;
}
}
// Horizontal screen .chart_lanscape {
height: 82vh;
font-size: 2vw;
text-align: center; .tbcls {
width: 98vw;
}
}
6、 Use ternary judgment data Medium isLandScape Is it false, If yes, vertical screen , Then use the vertical screen style , Otherwise, use horizontal screen style
<view :class="{
'chart_portrait': !isLandScape, 'chart_lanscape': isLandScape}">
</view>
Option two Enter the specified page to fix the dead horizontal screen , Automatically switch to vertical screen
explain : This scheme is relatively simple , Just enter and switch to horizontal screen , Come out and cut the vertical screen , But the root tag view The style needs to be modified , I am using vw/vh, It will automatically adapt to the height and width , You can also use percentages and rem, But don't use rpx, A pit1、 Or look at the renderings first

2、js
onLoad() {
// Enter the current page Automatically switch to fixed horizontal screen
// #ifdef APP-PLUS
plus.screen.lockOrientation("landscape-primary");
// #endif
},
onUnload() {
// When exiting the current page Automatically switch to vertical screen
// #ifdef APP-PLUS
plus.screen.lockOrientation("portrait-primary");
// #endif
},
3、css
// Horizontal screen .chart_lanscape {
height: 82vh;
font-size: 2vw;
text-align: center; // This is my table class name Make it as wide as the viewport 98% .tbcls {
width: 98vw;
}
}
The difference between the two schemes
The same thing : Is to specify a certain page , Both types of exit from the current page are automatically switched to the vertical screen mode
Difference :
The first one is : According to the user's mobile phone / Vertical automatic switching , But you need to write two sets of styles , One set horizontally and one set vertically ,
stay data Define a value to judge horizontal and vertical , Match again uniapp Page lifecycle onResize Listen for changes in the window , Transverse time isLandScape The initial value is true, Vertical false, To the outermost layer view The class style of the root tag uses ternary judgment to dynamically bind ,isLandScape The value of is true Use horizontal screen style ,false Use vertical screen style .
The second kind : Fixed dead horizontal screen when entering , Just write a set of class styles .
Be careful
Need horizontal / Try to use the vertical screen switching page Percentage layout and px , rem, There is rpx pit
ending
I have posted the source code of these two renderings on the code cloud , Interested friends can read or clone it locally and debug it with real machine to feel the horizontal and vertical screen ( I don't know how to debug the real machine , Click me to enter the tutorial ). I hope this article can help you , Thank you for watching .
Links to articles :https://blog.csdn.net/weixin_50880237/article/details/109848509
gitee Source code :https://gitee.com/lhs1303574731/uni-moblile-demo
边栏推荐
- Record the first article of so and so Xiao Lu
- Reptile learning summary
- NoSQL之Redis配置与优化
- Bert的实现方法
- Starting point Chinese website font anti crawling technology web page can display numbers and letters, and the web page code is garbled or blank
- Svg understanding and drawing application
- Branch and loop statements
- nodejs操作MongoDB
- A timed task reminder tool
- joern运行后.joernindex数据库无内容
猜你喜欢

最近最久未使用

Tailing microelectronics B91 general development board integrated into the trunk of openharmony community

Addition, deletion, check and modification of sequence table

shell---函数

Redis哨兵模式及集群

Serial port configuration of raspberry pie

js上传文件的方式

NAT network address translation

MySQL查询父节点下面的所有子孙节点,查询用户列表时多级(公司)部门处理,根据反射,递归树形结构工具类

vcf文件制作
随机推荐
ELK日志分析系统的部署
freemarker导出word,带表格和多张图片,解决图片重复和变形
js上传文件的方式
学生执勤问题
shell---sed语句练习
Multiprocessing (multiprocessing)
A timed task reminder tool
Basic usage and precautions of arrow function (= >) and three-point operator (...) in ES6 (this points to)
在转化词向量之前先转化为AST再转化为词向量的实现方法
Standard C language learning summary 8
MySQL excludes holidays and calculates the date difference
Static and floating routes
Pictures are adaptive to the screen
How did tensor leak your memory / video memory
Media set up live broadcast server
分解路径为目录名和文件名的方法
Nrf51822 review summary
C language review (modifier article)
Standard C language summary 4
Review of C language (byte alignment)