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

在react-native中如何获取View的漏出比例和漏出时间

写在前面

最近在项目中遇到了一个这样的需求:给一个模块做埋点,要求埋点的触发时机是当模块露出50%且停留300毫秒才进行上报

开搞

首先要有一个View

<View></View>

然后在View里定义一个ref

<View ref = { viewRef }></View>

然后写一个定时器,每隔300毫秒触发一次

setinterval(() => {// do something
}, 300)

然后判断viewRef是否露出50%,如果露出50%则触发埋点

import {Dimensions} from "react-native";this.state = {// 是否可埋点isExpoesd: false,// 是否停留300毫秒isStayThreeHundredMilliseconds: false,}
setinterval(() => {if(this.viewRef.current){this.viewRef.current.measure((x, y, width, height, pageX, pageY) => {// 获取屏幕高度let windowHeight = Dimensions.get('window').height;// 模块露出50%坐标值let moduleTop = windowHeight - (height / 2);// // 商品露出的高度// let exposeHeight = windowHeight - pageY;// // 卡片露出的高度// let cardPosHeight = windowHeight - pageY;// // 卡片露出的比例// let proportion = cardPosHeight / height;// if(exposeHeight > height){//     exposeHeight = '100%'// }// if(exposeHeight < 0){//     exposeHeight = `商品距离显示还有${Math.abs(exposeHeight)}`;//     proportion = '卡片还没露出呢!'// }// if(proportion > 1){//     proportion = '100%'// }// let tip = `//     这个商品露出了${moduleTop > pageY ? '大于50' : '小于50'},//     具体参数如下↓↓↓↓↓//     商品名称:${name},//     商品ID:${Id}//     屏幕高度:${windowHeight},//     模块整体高度:${height},//     模块最高点坐标:${pageY},//     如果模块坐标低于${moduleTop},说明露出大于50%,//     商品露出了:${exposeHeight}//     卡片露出的比例是:${proportion}//     `// console.log('------------------------分界线开始---------------------------------')// console.log(tip)this.setState(prevState => {let newIsExpoesd = moduleTop > pageY ? true : false;// 比较当前状态与新状态if (newIsExpoesd !== prevState.isExpoesd) {this.setState({isExpoesd: newIsExpoesd }); // 返回新的状态}// console.log(`Toggled state changed from ${prevState.isExpoesd} to ${newIsExpoesd}`);if((prevState.isExpoesd === newIsExpoesd) && (prevState.isExpoesd === true)){// console.log('可以埋点')this.state.isStayThreeHundredMilliseconds = true;return true} else {// console.log('不可以埋点')this.state.isStayThreeHundredMilliseconds = false;return false}});// console.log('------------------------分界线结束---------------------------------')});}
}, 300)
http://www.lryc.cn/news/462573.html

相关文章:

  • 谷歌新安装包文件形式 .aab 在UE4中的打包原理
  • 昂首平台:多货币专家顾问助力投资者优化外汇投资
  • Go标准库runtime.MemStats
  • MAC 电脑Office power point编辑的时候,显示“某些字体无法随演示文稿一起保存,仍然要保存演示文稿吗?”
  • R语言机器学习算法实战系列(四)随机森林算法+SHAP值 (Random Forest)
  • 用柔性神经k-Opt学习搜索路径问题的可行和不可行区域(未完,先看前驱文章L2S)
  • 【升华】人工智能python重要库scikit-learn学习
  • Stable Diffusion Web UI 大白话术语解释 (二)
  • vue-vben-admin 首页加载慢优化 升级vite2到vite3
  • 集合框架07:LinkedList使用
  • 一区鱼鹰优化算法+深度学习+注意力机制!OOA-TCN-LSTM-Attention多变量时间序列预测
  • Cesium 黑夜效果
  • leetcode动态规划(二)-斐波那契数列
  • 【MySQL】增删改查-进阶(一)
  • MacOS RocketMQ安装
  • OpenCV高级图形用户界面(6)获取指定窗口中图像的矩形区域函数getWindowImageRect()的使用
  • SpringColoud GateWay 核心组件
  • 5.计算机网络_抓包工具wireshark
  • 基于Java的车辆管理系统的设计与实现-计算机毕业设计源码41727
  • 在软件开发中低耦合和高内聚是什么,如何实现,请看文章
  • 关于MyBatis-Plus 提供Wrappers.lambdaQuery()的方法
  • C++——vector的了解与使用
  • Ubuntu设置静态IP地址
  • 力扣349.两个数组的交集
  • FreeRTOS - 软件定时器
  • Python的Atlassian第三方库的详细介绍
  • Java中的基本循环结构详解
  • 关于Git Bash中如何定义alias
  • luckfox1106初次使用
  • ab命令深入解析:ApacheBench性能测试工具