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

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()})

相关文章:

  • 取模--特殊情况讨论/数论
  • 机器学习--线性回归模型
  • Unity InputSystem触摸屏问题
  • 使用Tauri 2.3.1+Leptos 0.7.8开发桌面小程序汇总
  • 优雅实现网页弹窗提示功能:JavaScript与CSS完美结合
  • PyQt6基础_QTabWidget
  • 新增优惠券
  • 哈希表的实现
  • 大模型AI的“双刃剑“:数据安全与可靠性挑战与破局之道
  • 高精度并行2D圆弧拟合(C++)
  • ORACLE RAC环境使用ASM机制零宕机时间更换存储的实践
  • 安宝特案例 | AR技术在院外心脏骤停急救中的革命性应用
  • 4.4 记忆机制与上下文管理:短期与长期记忆的设计与应用
  • 新时代质量管理体系-端到端流程通俗演义,什么是端到端流程?
  • 3D高斯个人笔记
  • 如何实现客户端热部署能力方案
  • 写一个esp开发SPI的链接吗,
  • 机器人新革命:Pi 0.5如何让智能走进千家万户
  • 【踩坑记录】stm32 jlink程序烧录不进去
  • 电力作业安全工器具全解析:分类、配置与检查要点
  • 我国首次发布铁线礁、牛轭礁珊瑚礁“体检”报告,菲炮制言论毫无科学和事实依据
  • 昆明一小区电梯突然从40楼降到负4楼,回应:临时断电引起
  • 王宝强谈《棋士》:饰演这个灰度人物有一种被割裂的痛苦
  • 陕西省烹饪餐饮行业领军人物黄建军逝世,终年53岁
  • 梁启超“失肾记”的余波:中西医论战与最后的真相
  • 工程院院士应汉杰不再担任苏州大学校长