当前位置:   article > 正文

uniapp微信小程序横竖屏切换样式适配_uniapp竖屏和横屏适配

uniapp竖屏和横屏适配

一、首先明白要使用什么布局才能实现横竖屏适配?

 1、rpx布局是不能直接实现的,写两套(横屏、竖屏)样式才可以达到想要的效果

 2、使用:百分比、rem、vw\vh、vmin\vmax、px(px布局在不同设备上有差异) 都可以一套样式实现横竖屏适配

二、本文重点说css3的两个属性vmax和vmin实现适配:

 1、首先简单介绍下css3的两个属性vmax和vmin:

  1. vmax 相对于视口的宽度或高度中较大的那个。其中最大的那个被均分为100单位的vmax
  2. vmin 相对于视口的宽度或高度中较小的那个。其中最小的那个被均分为100单位的vmin

即:对于750rpx屏幕的宽度的手机,vmin不管横竖屏的情况下,100vmin都是手机屏幕的宽度

       公式:x rpx=( x * 100 / 750)vmin  即:10rpx = (10*100/750)vmin

使用scss声明tovmin函数:

  1. <style lang="scss" scoped>
  2. @function tovmin($rpx) {
  3. //$rpx为需要转换的字号
  4. @return #{$rpx * 100 / 750}vmin;
  5. }
  6. </style>

三、实现横竖屏适配demo:

1、效果图:

竖屏:


横屏:

 

2、代码:

首先配置开启小程序自带的横竖屏切换属性:

(1)单个页面固定横屏:

  1. {
  2. "path": "pages/pageTable/pageTable",
  3. "style": {
  4. "navigationBarTitleText": "表格",
  5. "mp-weixin": {
  6. "pageOrientation": "landscape"//横屏
  7. }
  8. }
  9. }
  10. //pageOrientation - 有三个值:auto:自动;landscape:横屏;portrait :竖屏

(2)全局设置所有页面开启,在pages.json文件全局样式中使用"pageOrientation": "auto"

  1. "globalStyle": {
  2. "navigationBarTextStyle": "black",
  3. "navigationBarTitleText": "uni-app",
  4. "navigationBarBackgroundColor": "#F8F8F8",
  5. "backgroundColor": "#F8F8F8",
  6. "pageOrientation": "auto"//横屏配置 auto自动 / portrait竖屏 / landscape横屏
  7. },

页面代码:

!!!直接复制运行可测:

  1. <template>
  2. <view>
  3. <view class="user-card">
  4. <view class="header">
  5. {{itemType!='1'?'康复项目查询':'家长签名'}}
  6. </view>
  7. <view class="row space-between titlre">
  8. <view>
  9. <text>住院号:</text>{{'2023001'}}
  10. </view>
  11. <view class="">
  12. <text>姓名:</text>{{'章小鱼'}}
  13. </view>
  14. <view class="">
  15. <text>性别:</text>
  16. </view>
  17. </view>
  18. </view>
  19. <view class="uni-container">
  20. <uni-table ref="table" :loading="loading" border type="selection" emptyText="暂无更多数据"
  21. @selection-change="selectionChange">
  22. <view>
  23. <uni-tr>
  24. <uni-th width="150" align="center">日期</uni-th>
  25. <uni-th width="150" align="center">姓名</uni-th>
  26. <uni-th width="180" align="center">地址</uni-th>
  27. <uni-th width="204" align="center">设置</uni-th>
  28. </uni-tr>
  29. </view>
  30. <scroll-view scroll-y="true" :style="[Style]">
  31. <uni-tr v-for="(item, index) in tableData" :key="index">
  32. <uni-td width="150" align="center">{{ item.date }}</uni-td>
  33. <uni-td width="150" align="center">
  34. <view class="name">{{ item.name }}</view>
  35. </uni-td>
  36. <uni-td width="180" align="center">{{ item.address }}</uni-td>
  37. <uni-td width="204" align="center">
  38. <view class="uni-group">
  39. <button class="uni-button" size="mini" type="primary">修改</button>
  40. <button class="uni-button" size="mini" type="warn">删除</button>
  41. </view>
  42. </uni-td>
  43. </uni-tr>
  44. </scroll-view>
  45. </uni-table>
  46. <view class="uni-pagination-box"><uni-pagination show-icon :page-size="pageSize" :current="pageCurrent"
  47. :total="total" @change="change" /></view>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. export default {
  53. data() {
  54. return {
  55. itemType: '1',
  56. scrollview_height: '',
  57. searchVal: '',
  58. tableData: [],
  59. // 每页数据量
  60. pageSize: 8,
  61. // 当前页
  62. pageCurrent: 1,
  63. // 数据总量
  64. total: 0,
  65. loading: false,
  66. tableList: [{
  67. "date": "2020-09-01",
  68. "name": "Dcloud1",
  69. "address": "上海市普陀区金沙江路 1518 弄"
  70. }, {
  71. "date": "2020-09-02",
  72. "name": "Dcloud2",
  73. "address": "上海市普陀区金沙江路 1517 弄"
  74. }, {
  75. "date": "2020-09-03",
  76. "name": "Dcloud3",
  77. "address": "上海市普陀区金沙江路 1519 弄"
  78. }, {
  79. "date": "2020-09-04",
  80. "name": "Dcloud4",
  81. "address": "上海市普陀区金沙江路 1516 弄"
  82. }, {
  83. "date": "2020-09-05",
  84. "name": "Dcloud5",
  85. "address": "上海市普陀区金沙江路 1518 弄"
  86. }, {
  87. "date": "2020-09-06",
  88. "name": "Dcloud6",
  89. "address": "上海市普陀区金沙江路 1517 弄"
  90. }, {
  91. "date": "2020-09-07",
  92. "name": "Dcloud7",
  93. "address": "上海市普陀区金沙江路 1519 弄"
  94. }, {
  95. "date": "2020-09-08",
  96. "name": "Dcloud8",
  97. "address": "上海市普陀区金沙江路 1516 弄"
  98. }, {
  99. "date": "2020-09-09",
  100. "name": "Dcloud9",
  101. "address": "上海市普陀区金沙江路 1518 弄"
  102. }, {
  103. "date": "2020-09-10",
  104. "name": "Dcloud10",
  105. "address": "上海市普陀区金沙江路 1517 弄"
  106. }, {
  107. "date": "2020-09-11",
  108. "name": "Dcloud11",
  109. "address": "上海市普陀区金沙江路 1519 弄"
  110. }, {
  111. "date": "2020-09-12",
  112. "name": "Dcloud12",
  113. "address": "上海市普陀区金沙江路 1516 弄"
  114. }, {
  115. "date": "2020-09-13",
  116. "name": "Dcloud13",
  117. "address": "上海市普陀区金沙江路 1518 弄"
  118. }, {
  119. "date": "2020-09-14",
  120. "name": "Dcloud14",
  121. "address": "上海市普陀区金沙江路 1517 弄"
  122. }, {
  123. "date": "2020-09-15",
  124. "name": "Dcloud15",
  125. "address": "上海市普陀区金沙江路 1519 弄"
  126. }, {
  127. "date": "2020-09-16",
  128. "name": "Dcloud16",
  129. "address": "上海市普陀区金沙江路 1516 弄"
  130. }, {
  131. "date": "2020-09-01",
  132. "name": "Dcloud17",
  133. "address": "上海市普陀区金沙江路 1518 弄"
  134. }, {
  135. "date": "2020-09-02",
  136. "name": "Dcloud18",
  137. "address": "上海市普陀区金沙江路 1517 弄"
  138. }, {
  139. "date": "2020-09-03",
  140. "name": "Dcloud19",
  141. "address": "上海市普陀区金沙江路 1519 弄"
  142. }, {
  143. "date": "2020-09-04",
  144. "name": "Dcloud20",
  145. "address": "上海市普陀区金沙江路 1516 弄"
  146. }, {
  147. "date": "2020-09-05",
  148. "name": "Dcloud21",
  149. "address": "上海市普陀区金沙江路 1518 弄"
  150. }, {
  151. "date": "2020-09-06",
  152. "name": "Dcloud22",
  153. "address": "上海市普陀区金沙江路 1517 弄"
  154. }, {
  155. "date": "2020-09-07",
  156. "name": "Dcloud23",
  157. "address": "上海市普陀区金沙江路 1519 弄"
  158. }, {
  159. "date": "2020-09-08",
  160. "name": "Dcloud24",
  161. "address": "上海市普陀区金沙江路 1516 弄"
  162. }, {
  163. "date": "2020-09-09",
  164. "name": "Dcloud25",
  165. "address": "上海市普陀区金沙江路 1518 弄"
  166. }, {
  167. "date": "2020-09-10",
  168. "name": "Dcloud26",
  169. "address": "上海市普陀区金沙江路 1517 弄"
  170. }, {
  171. "date": "2020-09-11",
  172. "name": "Dcloud27",
  173. "address": "上海市普陀区金沙江路 1519 弄"
  174. }, {
  175. "date": "2020-09-12",
  176. "name": "Dcloud28",
  177. "address": "上海市普陀区金沙江路 1516 弄"
  178. }, {
  179. "date": "2020-09-13",
  180. "name": "Dcloud29",
  181. "address": "上海市普陀区金沙江路 1518 弄"
  182. }, {
  183. "date": "2020-09-14",
  184. "name": "Dcloud30",
  185. "address": "上海市普陀区金沙江路 1517 弄"
  186. }, {
  187. "date": "2020-09-15",
  188. "name": "Dcloud31",
  189. "address": "上海市普陀区金沙江路 1519 弄"
  190. }, {
  191. "date": "2020-09-16",
  192. "name": "Dcloud32",
  193. "address": "上海市普陀区金沙江路 1516 弄"
  194. }, {
  195. "date": "2020-09-01",
  196. "name": "Dcloud33",
  197. "address": "上海市普陀区金沙江路 1518 弄"
  198. }, {
  199. "date": "2020-09-02",
  200. "name": "Dcloud34",
  201. "address": "上海市普陀区金沙江路 1517 弄"
  202. }, {
  203. "date": "2020-09-03",
  204. "name": "Dcloud35",
  205. "address": "上海市普陀区金沙江路 1519 弄"
  206. }, {
  207. "date": "2020-09-04",
  208. "name": "Dcloud36",
  209. "address": "上海市普陀区金沙江路 1516 弄"
  210. }, {
  211. "date": "2020-09-05",
  212. "name": "Dcloud37",
  213. "address": "上海市普陀区金沙江路 1518 弄"
  214. }, {
  215. "date": "2020-09-06",
  216. "name": "Dcloud38",
  217. "address": "上海市普陀区金沙江路 1517 弄"
  218. }, {
  219. "date": "2020-09-07",
  220. "name": "Dcloud39",
  221. "address": "上海市普陀区金沙江路 1519 弄"
  222. }, {
  223. "date": "2020-09-08",
  224. "name": "Dcloud40",
  225. "address": "上海市普陀区金沙江路 1516 弄"
  226. }, {
  227. "date": "2020-09-09",
  228. "name": "Dcloud41",
  229. "address": "上海市普陀区金沙江路 1518 弄"
  230. }, {
  231. "date": "2020-09-10",
  232. "name": "Dcloud42",
  233. "address": "上海市普陀区金沙江路 1517 弄"
  234. }, {
  235. "date": "2020-09-11",
  236. "name": "Dcloud43",
  237. "address": "上海市普陀区金沙江路 1519 弄"
  238. }, {
  239. "date": "2020-09-12",
  240. "name": "Dcloud44",
  241. "address": "上海市普陀区金沙江路 1516 弄"
  242. }, {
  243. "date": "2020-09-13",
  244. "name": "Dcloud45",
  245. "address": "上海市普陀区金沙江路 1518 弄"
  246. }, {
  247. "date": "2020-09-14",
  248. "name": "Dcloud46",
  249. "address": "上海市普陀区金沙江路 1517 弄"
  250. }, {
  251. "date": "2020-09-15",
  252. "name": "Dcloud47",
  253. "address": "上海市普陀区金沙江路 1519 弄"
  254. }, {
  255. "date": "2020-09-16",
  256. "name": "Dcloud48",
  257. "address": "上海市普陀区金沙江路 1516 弄"
  258. }]
  259. }
  260. },
  261. computed: {
  262. Style() {
  263. let obj = {
  264. "height": `${this.scrollview_height}px`
  265. }
  266. return obj
  267. },
  268. },
  269. onReady() { //比onload快
  270. this.setHeight()
  271. },
  272. onLoad() {
  273. this.selectedIndexs = []
  274. this.getData(1)
  275. },
  276. mounted() {
  277. // 监听屏幕方向的变化
  278. uni.onWindowResize(this.handleOrientationChange);
  279. },
  280. methods: {
  281. // 处理屏幕方向的变化
  282. handleOrientationChange() {
  283. const {
  284. screenWidth,
  285. screenHeight
  286. } = uni.getSystemInfoSync();
  287. const isLandscape = screenWidth > screenHeight;
  288. if (isLandscape) {
  289. // 横屏
  290. this.setHeight(31)
  291. } else {
  292. // 竖屏
  293. this.setHeight(88)
  294. }
  295. },
  296. setHeight(height=88){
  297. // 计算 scroll-view 的高度
  298. let userCard = 0
  299. let card = 0
  300. let bodyH = uni.getSystemInfoSync().windowHeight
  301. let query = uni.createSelectorQuery().in(this);
  302. query.select('.user-card').boundingClientRect(rect => {
  303. userCard = rect.height;
  304. console.log(bodyH,userCard,rect);
  305. this.scrollview_height = (bodyH - userCard - height);
  306. }).exec();
  307. },
  308. // 多选
  309. selectionChange(e) {
  310. console.log(e.detail.index)
  311. this.selectedIndexs = e.detail.index
  312. },
  313. // 分页触发
  314. change(e) {
  315. this.$refs.table.clearSelection()
  316. this.selectedIndexs.length = 0
  317. this.getData(e.current)
  318. },
  319. // 获取数据
  320. getData(pageCurrent, value = '') {
  321. this.loading = true
  322. this.pageCurrent = pageCurrent
  323. this.request({
  324. pageSize: this.pageSize,
  325. pageCurrent: pageCurrent,
  326. value: value,
  327. success: res => {
  328. console.log('data', res);
  329. this.tableData = res.data
  330. this.total = res.total
  331. this.loading = false
  332. }
  333. })
  334. },
  335. // 伪request请求
  336. request(options) {
  337. const {
  338. pageSize,
  339. pageCurrent,
  340. success,
  341. value
  342. } = options
  343. let total = this.tableList.length
  344. let data = this.tableList.filter((item, index) => {
  345. const idx = index - (pageCurrent - 1) * pageSize
  346. return idx < pageSize && idx >= 0
  347. })
  348. if (value) {
  349. data = []
  350. this.tableList.forEach(item => {
  351. if (item.name.indexOf(value) !== -1) {
  352. data.push(item)
  353. }
  354. })
  355. total = data.length
  356. }
  357. setTimeout(() => {
  358. typeof success === 'function' &&
  359. success({
  360. data: data,
  361. total: total
  362. })
  363. }, 500)
  364. }
  365. }
  366. }
  367. </script>
  368. <style lang="scss">
  369. @function tovmin($rpx) {
  370. //$rpx为需要转换的字号
  371. @return #{$rpx * 100 / 750}vmin;
  372. }
  373. .uni-group {
  374. display: flex;
  375. align-items: center;
  376. }
  377. .header {
  378. padding-top: tovmin(10);
  379. padding-left: tovmin(20);
  380. line-height: tovmin(60);
  381. font-size: tovmin(36);
  382. font-weight: 600;
  383. color: #2B2B2B;
  384. letter-spacing: tovmin(10);
  385. }
  386. .titlre {
  387. padding: tovmin(16) tovmin(22);
  388. text {
  389. font-size: tovmin(30);
  390. font-weight: 600;
  391. }
  392. }
  393. .u-td {
  394. height: auto;
  395. }
  396. .u-th {
  397. height: auto;
  398. }
  399. .row {
  400. display: flex;
  401. flex-direction: row;
  402. align-items: center;
  403. }
  404. .center {
  405. align-items: center;
  406. justify-content: center;
  407. }
  408. .end {
  409. justify-content: flex-end;
  410. }
  411. .space-between {
  412. justify-content: space-between;
  413. }
  414. .space-around {
  415. justify-content: space-around;
  416. }
  417. .column {
  418. display: flex;
  419. flex-direction: column;
  420. }
  421. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/152869
推荐阅读
相关标签
  

闽ICP备14008679号