当前位置: 首页 > news >正文

【taro react】---- 获取元素的位置和宽高等信息

1. 需求分析

  1. 添加节点的布局位置的查询请求。相对于显示区域,以像素为单位。其功能类似于 DOM 的 getBoundingClientRect。返回 NodesRef 对应的 SelectorQuery。
  2. 区分小程序和H5的环境,调用 getBoundingClientRect 获取对应的信息。

2. H5 实现

  1. 判断传入元素是否是window窗口,是window窗口,直接获取窗口的宽高;
  2. 元素,同时可以获取元素的宽高等信息;
  3. 都不满足,返回默认值。
function isWindow(val){return val === window
}export const getRect = (elementRef) => {const element = elementRef// 判断传入元素是否是window窗口,是window窗口,直接获取窗口的宽高if (isWindow(element)) {const width = element.innerWidthconst height = element.innerHeightreturn {top: 0,left: 0,right: width,bottom: height,width,height,}}// 是元素,同时可以获取元素的宽高等信息if (element && element.getBoundingClientRect) {return element.getBoundingClientRect()}// 都不满足,返回默认值return {top: 0,left: 0,right: 0,bottom: 0,width: 0,height: 0,}
}

3. Taro 实现

  1. 元素存在,判断使用对应环境获取元素信息;
  2. H5环境使用元素的获取元素信息方法;
  3. 微信小程序环境调用 boundingClientRect 获取元素信息;
  4. 返回默认值。
export const getRectByTaro = async (element) => {// 元素存在,判断使用对应环境获取元素信息if (element) {if(process.env.TARO_ENV === "h5"){// H5环境使用元素的获取元素信息方法return Promise.resolve(getRect(element))} else if(process.env.TARO_ENV === "weapp"){// 微信小程序环境调用 boundingClientRect 获取元素信息return new Promise((resolve) => {createSelectorQuery().select(`.${element.props.class.split(' ').filter(item => item).join('.')}`).boundingClientRect(resolve).exec()})}}// 返回默认值return Promise.resolve({top: 0,left: 0,right: 0,bottom: 0,width: 0,height: 0,})
}

4. 使用实例

1. 引入

import React, { Component } from 'react';
import { getRectByTaro } from '@utils/use-client-rect';

2. 获取 dom 的 getBoundingClientRect 信息

class page extends Component {constructor(props) {this.navbarRef = React.createRef()}componentDidShow(){// 如果元素内有动态信息,需要将获取信息的方法放到请求数据完成,设置数据后边this.getElementInfo()}getElementInfo(){let _this = this;let timer = setTimeout(async () => {clearTimeout(timer)console.log('_this.navbarRef',_this.navbarRef.current)let info = await getRectByTaro(_this.navbarRef.current)console.log('navbarRef', info)},0)}render() {return (<View className='rui-navbar-current-content' ref={this.navbarRef}></View>)}
}

5. 微信小程序返回样例

输入图片说明

6. H5 返回样例

输入图片说明

7. 完整代码

import { createSelectorQuery } from '@tarojs/taro';
function isWindow(val){return val === window
}export const getRect = (elementRef) => {const element = elementRef// 判断传入元素是否是window窗口,是window窗口,直接获取窗口的宽高if (isWindow(element)) {const width = element.innerWidthconst height = element.innerHeightreturn {top: 0,left: 0,right: width,bottom: height,width,height,}}// 是元素,同时可以获取元素的宽高等信息if (element && element.getBoundingClientRect) {return element.getBoundingClientRect()}// 都不满足,返回默认值return {top: 0,left: 0,right: 0,bottom: 0,width: 0,height: 0,}
}export const getRectByTaro = async (element) => {// 元素存在,判断使用对应环境获取元素信息if (element) {if(process.env.TARO_ENV === "h5"){// H5环境使用元素的获取元素信息方法return Promise.resolve(getRect(element))} else if(process.env.TARO_ENV === "weapp"){// 微信小程序环境调用 boundingClientRect 获取元素信息return new Promise((resolve) => {createSelectorQuery().select(`.${element.props.class.split(' ').filter(item => item).join('.')}`).boundingClientRect(resolve).exec()})}}// 返回默认值return Promise.resolve({top: 0,left: 0,right: 0,bottom: 0,width: 0,height: 0,})
}
http://www.lryc.cn/news/104910.html

相关文章:

  • Java【Spring】项目创建、存储和获取 Bean 的基本方式
  • docker minio安装
  • 设计模式-命令模式在Java中的使用示例-桌面程序自定义功能键
  • 分冶算法 剑指 07 重建二叉树 排序算法:剑指45 把数组排成最小的数 10-I 斐波那契数列
  • Postgresql取消正在执行的任务或强制终止正在执行的任务
  • 【Linux】Centos7 的 Systemctl 与 创建系统服务 (shell脚本)
  • Redis集群Cluster搭建
  • swing组件应用
  • Spring学习记录----十五、面向切面编程AOP+十六、Spring对事务的支持
  • Color Correction (颜色校正)
  • Unity-缓存池
  • ubuntu samba 配置常见问题
  • vue3.3-TinyMCE:TinyMCE富文本编辑器基础使用
  • 基于以太坊+IPFS的去中心化数据交易方法及平台
  • NestJS 的 拦截器 学习
  • Spring AOP 中的代理对象是怎么创建出来的?
  • 解决@Scope(“prototype“)不生效的问题
  • Mybatis 知识点
  • PHP中关于is,between,in等运算符的用法是什么?
  • 2023-07-29:华清远见嵌入式2017年线下班:文件IO笔记
  • 2023年第四届“华数杯”数学建模思路 - 复盘:光照强度计算的优化模型
  • Typescript第七章 处理错误(返回null,抛出异常,返回异常,Option类型)
  • Qt库xcb问题
  • C++ | 哈希表的实现与unordered_set/unordered_map的封装
  • 【漏洞挖掘】Xray+rad自动化批量漏洞挖掘
  • Swagger UI教程 API 文档和Node的使用
  • P5691 [NOI2001] 方程的解数
  • rust里用什么表示字节类型?
  • CMake简介
  • [threejs]相机与坐标