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

uniapp(vue3)动态计算swiper高度封装自定义hook

// useCalculateSwiperHeight.ts
import { ref, onMounted } from 'vue';export function useCalculateSwiperHeight(headerSelector: string = '.header-search', tabsWrapperSelector: string = '.u-tabs .u-tabs__wrapper') {const swiperHeight = ref<number>(0);// 封装uni.getSystemInfo为Promiseconst getSystemInfo = () => {return new Promise<UniApp.GetSystemInfoResult>((resolve) => {uni.getSystemInfo({ success: resolve });});};// 封装元素高度查询// 修复后的元素高度查询封装const getRect = (selector: string) => {return new Promise<UniNamespace.NodeInfo | null>((resolve) => {uni.createSelectorQuery().select(selector).boundingClientRect((res) => {// 处理可能返回数组的情况const result = Array.isArray(res) ? res[0] : res;resolve(result as UniNamespace.NodeInfo);}).exec();});};// 计算高度方法const calculateHeight = async () => {try {const [searchRect, tabsRect, sysInfo] = await Promise.all([getRect(headerSelector), getRect(tabsWrapperSelector), getSystemInfo()]);if (!searchRect || !tabsRect || !sysInfo) {console.error('未能获取到必要的布局信息');return;}swiperHeight.value = sysInfo.windowHeight - (searchRect.height || 0) - (tabsRect.height || 0);} catch (error) {console.error('计算高度时发生错误:', error);}};// 组件挂载后自动计算onMounted(() => {calculateHeight();});// 返回高度值和重新计算的方法return {swiperHeight,recalculate: calculateHeight};
}
  • 组件内使用
import { useCalculateSwiperHeight } from '@/hooks/useCalculateSwiperHeight';// #ifdef H5
const { swiperHeight } = useCalculateSwiperHeight('.header-search', '.u-tabs  .u-tabs__wrapper');
// #endif
// #ifdef MP-WEIXIN
const { swiperHeight } = useCalculateSwiperHeight('.header-search', '.u-tabs >>> .u-tabs__wrapper');
// #endif
http://www.xdnf.cn/news/5854.html

相关文章:

  • SD-HOST Controller design-----SD CLK 设计
  • 深度学习之优化器【从梯度下降到自适应学习率算法】(pytorch版)
  • 华为鸿蒙电脑能否作为开发机?开发非鸿蒙应用?
  • 微服务的“导航系统”:使用Spring Cloud Eureka实现服务注册与发现
  • 销售具备的能力有哪些
  • JAVA研发+前后端分离,ZKmall开源商城B2C商城如何保障系统性能?
  • Python中元组(Tuple)使用详解和注意事项
  • Kotlin 中的 Unit 类型的作用以及 Java 中 Void 的区别
  • 拓扑排序+dp
  • STM32-DMA数据转运(8)
  • 直接在Excel中用Python Matplotlib/Seaborn/Plotly......
  • Linux 内核网络协议栈:从 Socket 类型到协议注册的深度解析
  • 思迈特软件携手天阳科技,打造ChatBI金融智能分析新标杆
  • 适应性神经树:当深度学习遇上决策树的“生长法则”
  • Spring Boot 整合 Redis 实战
  • MySQL 事务(二)
  • 在 Qt Creator 中为 QDockWidget 设置隐藏和显示按钮
  • 中电金信参编的国家标准《信息技术 中间件 消息中间件技术要求》正式发布
  • 【爬虫】DrissionPage-1
  • 【TDengine源码阅读】#if defined(__APPLE__)
  • (C语言)超市管理系统(测试版)(指针)(数据结构)(二进制文件读写)
  • Android支持离线功能的复杂业务场景(如编辑、同步):设计数据同步策略的解决方案
  • 基于大模型的腰椎管狭窄术前、术中、术后全流程预测与治疗方案研究报告
  • 数据服务包括哪些内容?一文讲清数据服务模块的主要功能!
  • 【HarmonyOs鸿蒙】七种传参方式
  • IoTDB集群的一键启停功能详解
  • 裸机开发的核心技术:轮询、中断与DMA
  • PowerShell 实现 conda 懒加载
  • MUSE Pi Pro 编译kernel内核及创建自动化脚本进行环境配置
  • 什么是IoT长连接服务?