uni-app api 获取系统信息(高、宽)用法及封装
创始人
2024-06-01 01:43:52
0

uni-app提供了异步(uni.getSystemInfo)和同步(uni.getSystemInfoSync)的2个API获取系统信息

uniapp 官网解析地址

uni.getSystemInfo

异步获取系统信息

参数名类型必填说明
successFunction接口调用成功的回调
failFunction接口调用失败的回调函数
completeFunction接口调用结束的回调函数(调用成功、失败都会执行)

success返回其他的参数(详细所有内容看官网)
在这里插入图片描述

注意:

  • 屏幕高度 = 原生NavigationBar高度(含状态栏高度)+ 可使用窗口高度 + 原生TabBar高度
  • windowHeight不包含NavigationBar和TabBar的高度
  • Web端,windowTop等于NavigationBar高度,windowBottom等于TabBar高度
  • App端,windowTop等于透明状态NavigationBar高度,windowBottom等于透明状态TabBar高度
  • 高度相关信息,要放在 onReady 里获取。太早取不到。高度宽度的单位都是px
uni.getSystemInfo({success: function(res) {// *****高度宽度的单位都是px console.log(res.screenHeight); // 屏幕高度,包含导航栏console.log(res.windowHeight); // 能够使用的窗口高度,不包含导航栏console.log(res.screenWidth); // 屏幕宽度console.log(res.windowWidth); //  能够使用的窗口宽度console.log("windowHeight:",res.windowHeight)	let windowHeight = res.windowHeight;}
})

uni.getSystemInfoSync

获取系统信息的同步接口。调用参数和返回值同上getSystemInfo。

uni.getSystemInfoSync({success: function(res) {// *****高度宽度的单位都是px console.log(res.screenHeight); // 屏幕高度,包含导航栏console.log(res.windowHeight); // 能够使用的窗口高度,不包含导航栏console.log(res.screenWidth); // 屏幕宽度console.log(res.windowWidth); //  能够使用的窗口宽度console.log("windowHeight:",res.windowHeight)	let windowHeight = res.windowHeight;}
})

获取活动区域高度方法封装

/*** 获取活动区域高度* @param { nodeName } 节点名字* @param { nodeHeight } 节点高度* @param { nodeMPHeight } 节点margin 和 padding 的高度* */
function getHeight(nodeName, nodeHeight = 0,nodeMPHeight = 0) {let pageHeight = uni.getSystemInfoSync().windowHeight; // 获取当前页面的高度console.log("pageHeight: ",pageHeight)// 获取结节高度方法//  #ifdef H5const query = uni.createSelectorQuery();//  #endif//  #ifdef APPconst query = uni.createSelectorQuery().in(this)//  #endif// 获取当前结节高度值let nHeight = nodeHeightquery.select(nodeName).boundingClientRect(data => {console.log("data.height: ",data.height)nHeight = data.height}).exec();let nowHeight =  pageHeight - nHeight - nodeMPHeightconsole.log("nowHeight",nowHeight)return nowHeight + 'px'
}2. 调用
/*** 获取滚动高度--高度相关信息,要放在 onReady 里获取,太早取不到;* 单位都是px* 将rpx单位值转换成px* uni.upx2px(600) + 'px';* */			 
this.h = this.getHeight('#content-head', 0, uni.upx2px(20));

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...