当前位置:首页 > 技术文章 > 正文内容

Table滚动条到底能不能‘滚’到页面底部?”

zonemu3周前 (09-29)技术文章11

又是熟悉的对话

产品:"小王,这个表格用户体验不好啊,用户要滚动到底部才能看到横向滚动条,能不能把滚动条固定在页面底部?"

:"emmm... 这个... 技术上可以实现,但是..."

产品:"那就这么定了!明天上线!"

相信很多前端同学都遇到过类似的场景。面对超宽的 el-table,用户确实需要先滚动到表格底部才能进行左右滚动,体验确实不够友好。

干!

于是一个通用的水平滚动条组件 vue-horizontal-scrollbar 诞生了!

快速体验

想看看实际效果?访问 在线演示

最终的解决方案

<template>
<el-table style="width: 100%">
</el-table>

<HorizontalScrollbar
:target-selector="getSelector('.el-table__body-wrapper .el-scrollbar .el-scrollbar__wrap')"
:content-selector="getSelector('.el-table__body-wrapper .el-scrollbar .el-scrollbar__view')"
/>
</template>
<script setup>
import { ref } from 'vue'
import { VueHorizontalScrollbar } from 'vue-horizontal-scrollbar'
import "vue-horizontal-scrollbar/dist/style.css"
function getSelector(selector: string) {
const elements = document.querySelectorAll<HTMLElement>(selector) // 兼容展开行
if (elements.length) {
return elements[elements.length - 1]
}
else {
console.warn(`Selector "${selector}" did not match any elements.`)
return null
}
}
//  tips: 如果是有侧边菜单的管理系统需要动态修改vue-horizontal-scrollbar-container的left
</script>

这样做的好处

用户体验升级

  • 滚动条始终可见,无需滚动页面
  • 位置固定,操作便捷
  • 支持键盘和鼠标滚轮操作

开发体验友好

  • 一行代码解决问题
  • 不破坏原有组件结构
  • 支持任意 DOM 元素

高度可定制

<template>
<div>
<!-- Your scrollable content -->
<div id="scroll-container" style="overflow-x: auto; width: 100%;">
<div id="scroll-content" style="width: 2000px; height: 200px;">
<!-- Wide content here -->
<p>This content is wider than the container...</p>
</div>
</div>

<!-- Horizontal Scrollbar -->
<VueHorizontalScrollbar
target-selector="#scroll-container"
content-selector="#scroll-content"
:auto-show="true"
@scroll="onScroll"
/>
</div>
</template>
<script setup>
import { VueHorizontalScrollbar } from 'vue-horizontal-scrollbar'
import "vue-horizontal-scrollbar/dist/style.css"
function onScroll(info) {
console.log('Scroll info:', info)
// { scrollLeft: 100, maxScroll: 1000, scrollPercent: 10 }
}
</script>

Features

  • Vue 3 & TypeScript - Full TypeScript support with Vue 3 Composition API
  • Customizable - Flexible styling and configuration options
  • Accessible - ARIA labels and keyboard navigation support
  • Touch Friendly - Mobile-friendly touch gestures
  • Performance - Optimized with throttling and efficient updates
  • Flexible - Works with any scrollable content
  • Event Rich - Comprehensive event system for interactions
  • Lightweight - Minimal dependencies

API Reference

Props

Prop

Type

Default

Description

targetSelector

string | Function

Required. CSS selector or function returning the scroll container element

contentSelector

string | Function

Required. CSS selector or function returning the content element

autoShow

boolean

true

Auto show/hide scrollbar based on content width

minScrollDistance

number

50

Minimum scroll distance to show scrollbar (when autoShow is true)

height

number

16

Scrollbar height in pixels

enableKeyboard

boolean

true

Enable keyboard navigation (Arrow keys, Home, End)

scrollStep

number

50

Scroll step for keyboard navigation

minThumbWidth

number

30

Minimum thumb width in pixels

throttleDelay

number

16

Throttle delay for scroll events in milliseconds

zIndex

number

9999

Z-index for the scrollbar

disabled

boolean

false

Disable the scrollbar

ariaLabel

string

'Horizontal scrollbar'

ARIA label for accessibility

teleportTo

string

'body'

Teleport to target element


更多有趣的玩法

除了解决表格滚动问题,这个组件还能用在:

  • 商品展示:电商网站的商品横向滚动
  • 图片画廊:摄影作品展示
  • 时间轴:项目进度展示
  • 标签导航:当标签太多时的横向滚动

立即使用

bash

npm install vue-horizontal-scrollbar

结语

从此以后,再也不怕产品提这种"奇葩"需求了!

产品:"这个滚动条能不能再加个渐变效果?"

:"没问题!改个 CSS 就行!"

产品:"能不能支持触摸滑动?"

:"早就支持了!"

相关文章

Vue3快速入门(vue3快速上手)

  1.核心语法  1. 1选项式和组合式的区别  Vue2的API设计是Options(选项)风格的。  Vue3的API设计是Composition(组合)风格的。  Options类型的 API...

高效使用 Vim 编辑器的 10 个技巧

在 Reverb,我们使用 MacVim 来标准化开发环境,使配对更容易,并提高效率。当我开始使用 Reverb 时,我以前从未使用过 Vim。我花了几个星期才开始感到舒服,但如果没有这样的提示,可能...

html5你能把太阳系动态做出来,但是你能把月亮也做出来吗?

需要源码请评论后加前端学习群470593776课题:HTML5加原生js打造一个炫酷动态的太阳系简介:首先对于太阳系各大星球的运转关系,速度等资料,不然弄出来也是被喷的下场, 还有对于逻辑思维,算法的...

一键看懂Html5,就这么简单(查看html的app推荐)

HTML5是WEB开发世界的一次重大的改变,事实上不管你是否喜欢,它都是代表着未来趋势。曾几何时,当HTML5出现在web端开发领域的时候,并没有引起太多人的注意,究其原因,一方面是它还没有被广泛的支...

HTML5与APP的抉择(h5与app的区别)

同为当下炙手可热的技术,围绕APP和HTML5难免少不了各种争辩。而在“互联网+”时代,许多面临转型的传统企业,也在选择转型工具时,陷入了HTML5或APP的纠结抉择之中……到底该选择HTML5还是A...

育知HTML5培训,为什么要学习“HTML5混合式开发技术”

HTML5 的广泛应用,强势崛起企业现在安卓、iOS开发人员都在学习HTML5混合开发,节约成本、一专多能是未来很多企业用人趋势!HTML5工程师在今后的工作中与 Android、iOS工程师对接的几...