uni-app api 获取系统信息(高、宽)用法及封装

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 H5
	const query = uni.createSelectorQuery();
	//  #endif
	//  #ifdef APP
	const query = uni.createSelectorQuery().in(this)
	//  #endif
	// 获取当前结节高度值
	let nHeight = nodeHeight
	query
		.select(nodeName)
		.boundingClientRect(data => {
			console.log("data.height: ",data.height)
			nHeight = data.height
		})
		.exec();
	let nowHeight =  pageHeight - nHeight - nodeMPHeight
	console.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));
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值