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

vue3+TS 手动实现表格滚动

第一步定义变量

  // 定义滚动定时器

  const scrollInterval = ref<number | null>(null)

  // 定义是否悬浮

  const isHovered = ref(false)

  // 定义初始化定时器

  const initTimer = ref<number | null>(null)

  // 定义底部暂停定时器

  const bottomPauseTimer = ref<number | null>(null)

  // 定义是否滚动到底部

  const isAtBottom = ref(false)

  // 是否滚动中

  const isScrolling = ref(false)

第二部再html部分撰写内容

<divclass="content-box"ref="contentBoxRef"@mouseenter="isHovered = true"@mouseleave="isHovered = false"><div v-for="(item, index) in props.dataList" :key="index" class="content-item"><template v-for="(row, number) in props.headerList" :key="row.key"><div class="row-item" :style="getRowItemStyle(row)"><img v-if="!number" class="ranking-item" :src="getImg(index)" alt="排位" /><span class="ellipsis-text">{{ item[row.key] }}</span></div></template></div></div>

第三步进行js代码部分撰写

/*** @description: 启动自动滚动* @return {void}*/const startAutoScroll = () => {if (scrollInterval.value || isScrolling.value) return// 初始延迟5秒isScrolling.value = falseinitTimer.value = window.setTimeout(() => {isScrolling.value = truedoScroll()}, 3000)}/*** @description: 执行滚动* @return {void}*/const doScroll = () => {if (scrollInterval.value) returnscrollInterval.value = window.setInterval(() => {if (!contentBoxRef.value || isHovered.value || !isScrolling.value) returnconst { scrollTop, scrollHeight, clientHeight } = contentBoxRef.valueconst canScroll = scrollHeight > clientHeightconst reachedBottom = scrollTop + clientHeight >= scrollHeight - 1if (canScroll) {if (reachedBottom && !isAtBottom.value) {// 到达底部,暂停5秒isAtBottom.value = trueisScrolling.value = falseif (scrollInterval.value) clearInterval(scrollInterval.value)scrollInterval.value = nullbottomPauseTimer.value = window.setTimeout(() => {// 暂停结束后回到顶部contentBoxRef.value!.scrollTop = 0isAtBottom.value = falseisScrolling.value = falsestartAutoScroll() // 重新开始滚动}, 3000)} else if (!reachedBottom) {// 正常滚动contentBoxRef.value.scrollBy({top: 1,behavior: 'smooth',})}}}, 50) as unknown as number}/*** @description: 停止所有滚动和定时器* @return {void}*/const stopAllScroll = () => {if (initTimer.value) {clearTimeout(initTimer.value)initTimer.value = null}if (bottomPauseTimer.value) {clearTimeout(bottomPauseTimer.value)bottomPauseTimer.value = null}if (scrollInterval.value) {clearInterval(scrollInterval.value)scrollInterval.value = null}isScrolling.value = falseisAtBottom.value = false}// 调用接口之后删除onMounted(() => {// 启动自动滚动(会有初始5秒延迟)startAutoScroll()})onUnmounted(() => {// 组件卸载时清除所有定时器stopAllScroll()})

http://www.xdnf.cn/news/1739.html

相关文章:

  • C#进阶学习(十二)协变逆变
  • [C#]反射的实战应用,实际数据模拟
  • CDGP|大模型赋能数据治理:实践案例与深度剖析
  • VITS:基于对抗学习的条件变分自编码器
  • Java大师成长计划之第2天:面向对象编程在Java中的应用
  • 【回眸】Aurix TC397 IST 以太网 UDP 相关开发
  • 【python】Python 中,单下划线(_)和双下划线(__)开头以及结尾的命名方式具有特殊的含义和用途
  • 每日算法——快乐数、两数之和
  • 域控重命名导致无法登录
  • 回溯--一种暴力搜索算法
  • write函数
  • RTSP播放器实现回调RGB|YUV给视觉算法,然后二次编码推送到RTMP服务
  • ORACLE DATAGUARD遇到GAP增量恢复方式修复RAC环境备机的实践
  • C语言教程(十五):C 语言函数指针与回调函数详解
  • 【高并发】 MySQL锁优化策略
  • rsync实现内网两台服务器文件同步
  • Winddows11官网下载安装VMware Workstation Pro17(图文详解)
  • Linux命令-perf
  • 企业办公即时通讯软件BeeWorks,私有化安全防泄密
  • 【MobaXterm】---修改 MobaXterm 终端 默认字体和大小 保真
  • 基于 C++ 的用户认证系统开发:从注册登录到Redis 缓存优化
  • 【技术派后端篇】整合WebSocket长连接实现消息实时推送
  • 《Python3网络爬虫开发实战(第二版)》配套案例 spa6
  • 数据结构——栈与队列
  • GPU热设计功耗(TDP)与计算效率的平衡艺术:动态频率调节对算法收敛速度的影响量化分析
  • 【Leetcode 每日一题】2799. 统计完全子数组的数目
  • Spring Security结构总览
  • 网络变更:APIC 节点替换
  • 使用Tauri 2.3.1+Leptos 0.7.8开发桌面小程序汇总
  • 【多智能体系统组织方式解析】五大架构赋能智能协作