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));

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...