@leslie1900/el-table-horizontal-scroll 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Wang Ming
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # el-table-horizontal-scroll
2
+
3
+ 一个专门为 Element Plus Table (Vue 3) 优化横向滚动条体验的自定义指令。
4
+
5
+ ## 特性
6
+
7
+ - 🚀 **悬浮式滚动条**:当表格超出屏幕宽度且底部在视口外时,会在屏幕底部生成一个固定的横向滚动条,方便用户在任何位置进行横向滚动。
8
+ - 🖱️ **完美交互**:支持在悬浮滚动条上直接点击轨道进行平滑滚动、拖动滑块进行任意位置滚动,并与表格原生的横向滚动保持完美的双向同步。
9
+ - 🍃 **自动销毁**:自动监听容器大小变化与可见性变化,并在指令解绑时释放所有 DOM 节点与事件监听器,避免内存泄漏。
10
+ - 📦 **开箱即用**:支持 Vue 3 全局注册与局部导入。
11
+
12
+ ## 安装
13
+
14
+ 使用 npm 或 pnpm 安装:
15
+
16
+ ```bash
17
+ npm install el-table-horizontal-scroll
18
+ # 或者
19
+ pnpm add el-table-horizontal-scroll
20
+ ```
21
+
22
+ ## 使用方式
23
+
24
+ ### 1. 全局注册
25
+
26
+ 在入口文件(例如 `main.ts` 或 `main.js`)中注册该插件:
27
+
28
+ ```typescript
29
+ import { createApp } from 'vue'
30
+ import App from './App.vue'
31
+ import ElTableHorizontalScroll from 'el-table-horizontal-scroll'
32
+
33
+ const app = createApp(App)
34
+ app.use(ElTableHorizontalScroll)
35
+ app.mount('#app')
36
+ ```
37
+
38
+ ### 2. 局部注册
39
+
40
+ 在单文件组件(SFC)中按需引入:
41
+
42
+ ```vue
43
+ <script setup lang="ts">
44
+ import { tableHorizontalScrollDirective as vTableHorizontalScroll } from 'el-table-horizontal-scroll'
45
+ </script>
46
+
47
+ <template>
48
+ <el-table v-table-horizontal-scroll ...>
49
+ <!-- ... -->
50
+ </el-table>
51
+ </template>
52
+ ```
53
+
54
+ ### 3. 指令配置选项
55
+
56
+ 可以通过指令绑定的值来控制滚动条的显示行为:
57
+
58
+ - **鼠标悬浮显示(默认)**:
59
+ ```html
60
+ <el-table v-table-horizontal-scroll>...</el-table>
61
+ <!-- 或者传入 'hover' -->
62
+ <el-table v-table-horizontal-scroll="'hover'">...</el-table>
63
+ ```
64
+ 在鼠标移入表格时显示滚动滑块,移出时隐藏。
65
+
66
+ - **始终显示**:
67
+ ```html
68
+ <el-table v-table-horizontal-scroll="'always'">...</el-table>
69
+ ```
70
+ 只要表格存在横向滚动且底部处于视口下方,便一直显示悬浮滚动条。
71
+
72
+ - **禁用优化**:
73
+ ```html
74
+ <el-table v-table-horizontal-scroll="false">...</el-table>
75
+ ```
76
+ 传入 `false` 可在特定场景下动态禁用该指令。
77
+
78
+ ## 开源协议
79
+
80
+ [MIT LICENSE](LICENSE)
package/dist/index.cjs ADDED
@@ -0,0 +1,40 @@
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const V=()=>{const e="el-table-horizontal-scrollbar-style";if(document.getElementById(e))return;const t=document.createElement("style");t.id=e,t.innerHTML=`
2
+ .el-table-horizontal-scrollbar {
3
+ position: fixed !important;
4
+ bottom: 0 !important;
5
+ height: 6px !important;
6
+ background-color: transparent !important;
7
+ z-index: 1000 !important;
8
+ cursor: pointer !important;
9
+ opacity: 0 !important;
10
+ pointer-events: none !important;
11
+ transform: scaleY(1) translateY(0) !important;
12
+ transform-origin: bottom !important;
13
+ transition: opacity 0.3s cubic-bezier(0.25, 1, 0.5, 1),
14
+ transform 0.3s cubic-bezier(0.25, 1, 0.5, 1),
15
+ background-color 0.3s cubic-bezier(0.25, 1, 0.5, 1) !important;
16
+ }
17
+ .el-table-horizontal-scrollbar.is-active {
18
+ opacity: 0.4 !important;
19
+ pointer-events: auto !important;
20
+ }
21
+ .el-table-horizontal-scrollbar.is-active:hover {
22
+ opacity: 0.8 !important;
23
+ transform: scaleY(1.5) translateY(-10%) !important;
24
+ background-color: rgba(0, 0, 0, 0.02) !important;
25
+ }
26
+ .el-table-horizontal-scrollbar__thumb {
27
+ position: absolute !important;
28
+ top: 0 !important;
29
+ left: 0 !important;
30
+ height: 100% !important;
31
+ border-radius: 4px !important;
32
+ background-color: var(--el-text-color-placeholder, rgba(144, 147, 153, 0.35)) !important;
33
+ cursor: pointer !important;
34
+ transform: translateX(0);
35
+ transition: background-color 0.2s ease !important;
36
+ }
37
+ .el-table-horizontal-scrollbar__thumb:hover {
38
+ background-color: var(--el-text-color-secondary, rgba(144, 147, 153, 0.55)) !important;
39
+ }
40
+ `,document.head.appendChild(t)},T=(e,t)=>{if(t.value===!1)return;V();const o=e.querySelector(".el-scrollbar__wrap"),E=e.querySelector(".el-scrollbar");if(!o||!E||e.__tableScrollData)return;const n=document.createElement("div");n.className="el-table-horizontal-scrollbar";const l=document.createElement("div");l.className="el-table-horizontal-scrollbar__thumb",n.appendChild(l),e.appendChild(n);const u=document.createElement("div");u.className="el-table-horizontal-scrollbar-sentinel",Object.assign(u.style,{height:"1px",marginTop:"-1px",visibility:"hidden",pointerEvents:"none"}),e.appendChild(u);const g=t.value==="always"?"always":"hover";let h=!1,y=!1,z=!1;const k=()=>{const r=o.clientWidth,i=o.scrollWidth,a=r*100/i;l.style.width=`${Math.min(a,100)}%`},S=()=>{const r=o.clientWidth,i=o.scrollWidth,a=o.scrollLeft,s=i-r,m=n.clientWidth-l.clientWidth;if(s<=0||m<=0){l.style.transform="translateX(0)";return}const d=a/s*m;l.style.transform=`translateX(${d}px)`},c=()=>{if(!o||!n)return;const r=E.getBoundingClientRect();o.scrollWidth>o.clientWidth&&y&&!z&&(g==="always"||h)?(n.classList.add("is-active"),n.style.width=`${r.width}px`,n.style.left=`${r.left}px`,k(),S()):n.classList.remove("is-active")},W=()=>{S()};o.addEventListener("scroll",W);const _=()=>{h=!0,c()},x=()=>{h=!1,c()};e.addEventListener("mouseenter",_),e.addEventListener("mouseleave",x);const O=r=>{if(r.ctrlKey||r.button===2)return;r.stopImmediatePropagation(),r.preventDefault();const i=r.clientX,a=o.scrollLeft,s=o.scrollWidth-o.clientWidth,m=n.clientWidth-l.clientWidth;if(s<=0||m<=0)return;const d=s/m,p=L=>{const X=L.clientX-i;o.scrollLeft=a+X*d},v=()=>{document.removeEventListener("mousemove",p),document.removeEventListener("mouseup",v),document.onselectstart=null};document.addEventListener("mousemove",p),document.addEventListener("mouseup",v),document.onselectstart=()=>!1};l.addEventListener("mousedown",O);const w=r=>{if(r.target===l)return;r.preventDefault();const i=n.getBoundingClientRect(),a=r.clientX-i.left,s=l.clientWidth,m=a-s/2,d=n.clientWidth-s,p=Math.max(0,Math.min(d,m)),v=o.scrollWidth-o.clientWidth;if(v<=0||d<=0)return;const L=v/d;o.scrollTo({left:p*L,behavior:"smooth"})};n.addEventListener("click",w);const I=new IntersectionObserver(([r])=>{y=r.isIntersecting,c()},{threshold:0});I.observe(e);const M=new IntersectionObserver(([r])=>{z=r.isIntersecting,c()},{threshold:0});M.observe(u);let b=null;const D=()=>{b===null&&(b=requestAnimationFrame(()=>{c(),b=null}))},R=()=>{b!==null&&(cancelAnimationFrame(b),b=null)},f=new ResizeObserver(()=>{D()});f.observe(e),f.observe(o),e.__tableScrollData={scrollWrap:o,barContainer:n,thumbEl:l,bottomIsVisibleObserverEl:u,tableIntersectionObserver:I,bottomIsVisibleObserver:M,resizeObserver:f,onTableScroll:W,onDragStart:O,onBarClick:w,onMouseEnter:_,onMouseLeave:x,updatePosition:c,cancelResize:R,mode:g,isMouseOverTable:h},c()},H=e=>{const t=e.__tableScrollData;t&&(t.tableIntersectionObserver&&t.tableIntersectionObserver.disconnect(),t.bottomIsVisibleObserver&&t.bottomIsVisibleObserver.disconnect(),t.resizeObserver&&t.resizeObserver.disconnect(),t.cancelResize&&t.cancelResize(),t.scrollWrap&&t.scrollWrap.removeEventListener("scroll",t.onTableScroll),t.onMouseEnter&&e.removeEventListener("mouseenter",t.onMouseEnter),t.onMouseLeave&&e.removeEventListener("mouseleave",t.onMouseLeave),t.thumbEl&&t.thumbEl.removeEventListener("mousedown",t.onDragStart),t.barContainer&&(t.barContainer.removeEventListener("click",t.onBarClick),t.barContainer.remove()),t.bottomIsVisibleObserverEl&&t.bottomIsVisibleObserverEl.remove(),delete e.__tableScrollData)},C={mounted(e,t){setTimeout(()=>{T(e,t)},150)},updated(e,t){setTimeout(()=>{const o=e.__tableScrollData;o?(o.mode=t.value==="always"?"always":"hover",o.updatePosition()):T(e,t)},150)},unmounted(e){H(e)}},P=e=>{e.directive("table-horizontal-scroll",C)},B={install:P};exports.default=B;exports.tableHorizontalScrollDirective=C;
@@ -0,0 +1,30 @@
1
+ import { App, Directive } from 'vue';
2
+
3
+ interface TableScrollData {
4
+ scrollWrap: HTMLElement;
5
+ barContainer: HTMLElement;
6
+ thumbEl: HTMLElement;
7
+ bottomIsVisibleObserverEl: HTMLElement;
8
+ tableIntersectionObserver: IntersectionObserver;
9
+ bottomIsVisibleObserver: IntersectionObserver;
10
+ resizeObserver: ResizeObserver;
11
+ onTableScroll: () => void;
12
+ onDragStart: (e: MouseEvent) => void;
13
+ onBarClick: (e: MouseEvent) => void;
14
+ onMouseEnter: () => void;
15
+ onMouseLeave: () => void;
16
+ updatePosition: () => void;
17
+ cancelResize: () => void;
18
+ mode: 'hover' | 'always';
19
+ isMouseOverTable: boolean;
20
+ }
21
+ declare global {
22
+ interface HTMLElement {
23
+ __tableScrollData?: TableScrollData;
24
+ }
25
+ }
26
+ export declare const tableHorizontalScrollDirective: Directive;
27
+ declare const _default: {
28
+ install: (app: App<any>) => void;
29
+ };
30
+ export default _default;
package/dist/index.js ADDED
@@ -0,0 +1,190 @@
1
+ const X = () => {
2
+ const e = "el-table-horizontal-scrollbar-style";
3
+ if (document.getElementById(e))
4
+ return;
5
+ const t = document.createElement("style");
6
+ t.id = e, t.innerHTML = `
7
+ .el-table-horizontal-scrollbar {
8
+ position: fixed !important;
9
+ bottom: 0 !important;
10
+ height: 6px !important;
11
+ background-color: transparent !important;
12
+ z-index: 1000 !important;
13
+ cursor: pointer !important;
14
+ opacity: 0 !important;
15
+ pointer-events: none !important;
16
+ transform: scaleY(1) translateY(0) !important;
17
+ transform-origin: bottom !important;
18
+ transition: opacity 0.3s cubic-bezier(0.25, 1, 0.5, 1),
19
+ transform 0.3s cubic-bezier(0.25, 1, 0.5, 1),
20
+ background-color 0.3s cubic-bezier(0.25, 1, 0.5, 1) !important;
21
+ }
22
+ .el-table-horizontal-scrollbar.is-active {
23
+ opacity: 0.4 !important;
24
+ pointer-events: auto !important;
25
+ }
26
+ .el-table-horizontal-scrollbar.is-active:hover {
27
+ opacity: 0.8 !important;
28
+ transform: scaleY(1.5) translateY(-10%) !important;
29
+ background-color: rgba(0, 0, 0, 0.02) !important;
30
+ }
31
+ .el-table-horizontal-scrollbar__thumb {
32
+ position: absolute !important;
33
+ top: 0 !important;
34
+ left: 0 !important;
35
+ height: 100% !important;
36
+ border-radius: 4px !important;
37
+ background-color: var(--el-text-color-placeholder, rgba(144, 147, 153, 0.35)) !important;
38
+ cursor: pointer !important;
39
+ transform: translateX(0);
40
+ transition: background-color 0.2s ease !important;
41
+ }
42
+ .el-table-horizontal-scrollbar__thumb:hover {
43
+ background-color: var(--el-text-color-secondary, rgba(144, 147, 153, 0.55)) !important;
44
+ }
45
+ `, document.head.appendChild(t);
46
+ }, M = (e, t) => {
47
+ if (t.value === !1)
48
+ return;
49
+ X();
50
+ const o = e.querySelector(".el-scrollbar__wrap"), E = e.querySelector(".el-scrollbar");
51
+ if (!o || !E || e.__tableScrollData)
52
+ return;
53
+ const n = document.createElement("div");
54
+ n.className = "el-table-horizontal-scrollbar";
55
+ const s = document.createElement("div");
56
+ s.className = "el-table-horizontal-scrollbar__thumb", n.appendChild(s), e.appendChild(n);
57
+ const u = document.createElement("div");
58
+ u.className = "el-table-horizontal-scrollbar-sentinel", Object.assign(u.style, {
59
+ height: "1px",
60
+ marginTop: "-1px",
61
+ visibility: "hidden",
62
+ pointerEvents: "none"
63
+ }), e.appendChild(u);
64
+ const y = t.value === "always" ? "always" : "hover";
65
+ let v = !1, g = !1, z = !1;
66
+ const C = () => {
67
+ const r = o.clientWidth, i = o.scrollWidth, a = r * 100 / i;
68
+ s.style.width = `${Math.min(a, 100)}%`;
69
+ }, W = () => {
70
+ const r = o.clientWidth, i = o.scrollWidth, a = o.scrollLeft, l = i - r, m = n.clientWidth - s.clientWidth;
71
+ if (l <= 0 || m <= 0) {
72
+ s.style.transform = "translateX(0)";
73
+ return;
74
+ }
75
+ const d = a / l * m;
76
+ s.style.transform = `translateX(${d}px)`;
77
+ }, c = () => {
78
+ if (!o || !n)
79
+ return;
80
+ const r = E.getBoundingClientRect();
81
+ o.scrollWidth > o.clientWidth && g && !z && (y === "always" || v) ? (n.classList.add("is-active"), n.style.width = `${r.width}px`, n.style.left = `${r.left}px`, C(), W()) : n.classList.remove("is-active");
82
+ }, S = () => {
83
+ W();
84
+ };
85
+ o.addEventListener("scroll", S);
86
+ const x = () => {
87
+ v = !0, c();
88
+ }, _ = () => {
89
+ v = !1, c();
90
+ };
91
+ e.addEventListener("mouseenter", x), e.addEventListener("mouseleave", _);
92
+ const w = (r) => {
93
+ if (r.ctrlKey || r.button === 2)
94
+ return;
95
+ r.stopImmediatePropagation(), r.preventDefault();
96
+ const i = r.clientX, a = o.scrollLeft, l = o.scrollWidth - o.clientWidth, m = n.clientWidth - s.clientWidth;
97
+ if (l <= 0 || m <= 0)
98
+ return;
99
+ const d = l / m, p = (L) => {
100
+ const R = L.clientX - i;
101
+ o.scrollLeft = a + R * d;
102
+ }, h = () => {
103
+ document.removeEventListener("mousemove", p), document.removeEventListener("mouseup", h), document.onselectstart = null;
104
+ };
105
+ document.addEventListener("mousemove", p), document.addEventListener("mouseup", h), document.onselectstart = () => !1;
106
+ };
107
+ s.addEventListener("mousedown", w);
108
+ const I = (r) => {
109
+ if (r.target === s)
110
+ return;
111
+ r.preventDefault();
112
+ const i = n.getBoundingClientRect(), a = r.clientX - i.left, l = s.clientWidth, m = a - l / 2, d = n.clientWidth - l, p = Math.max(0, Math.min(d, m)), h = o.scrollWidth - o.clientWidth;
113
+ if (h <= 0 || d <= 0)
114
+ return;
115
+ const L = h / d;
116
+ o.scrollTo({
117
+ left: p * L,
118
+ behavior: "smooth"
119
+ });
120
+ };
121
+ n.addEventListener("click", I);
122
+ const O = new IntersectionObserver(
123
+ ([r]) => {
124
+ g = r.isIntersecting, c();
125
+ },
126
+ { threshold: 0 }
127
+ );
128
+ O.observe(e);
129
+ const T = new IntersectionObserver(
130
+ ([r]) => {
131
+ z = r.isIntersecting, c();
132
+ },
133
+ { threshold: 0 }
134
+ );
135
+ T.observe(u);
136
+ let b = null;
137
+ const k = () => {
138
+ b === null && (b = requestAnimationFrame(() => {
139
+ c(), b = null;
140
+ }));
141
+ }, D = () => {
142
+ b !== null && (cancelAnimationFrame(b), b = null);
143
+ }, f = new ResizeObserver(() => {
144
+ k();
145
+ });
146
+ f.observe(e), f.observe(o), e.__tableScrollData = {
147
+ scrollWrap: o,
148
+ barContainer: n,
149
+ thumbEl: s,
150
+ bottomIsVisibleObserverEl: u,
151
+ tableIntersectionObserver: O,
152
+ bottomIsVisibleObserver: T,
153
+ resizeObserver: f,
154
+ onTableScroll: S,
155
+ onDragStart: w,
156
+ onBarClick: I,
157
+ onMouseEnter: x,
158
+ onMouseLeave: _,
159
+ updatePosition: c,
160
+ cancelResize: D,
161
+ mode: y,
162
+ isMouseOverTable: v
163
+ }, c();
164
+ }, V = (e) => {
165
+ const t = e.__tableScrollData;
166
+ t && (t.tableIntersectionObserver && t.tableIntersectionObserver.disconnect(), t.bottomIsVisibleObserver && t.bottomIsVisibleObserver.disconnect(), t.resizeObserver && t.resizeObserver.disconnect(), t.cancelResize && t.cancelResize(), t.scrollWrap && t.scrollWrap.removeEventListener("scroll", t.onTableScroll), t.onMouseEnter && e.removeEventListener("mouseenter", t.onMouseEnter), t.onMouseLeave && e.removeEventListener("mouseleave", t.onMouseLeave), t.thumbEl && t.thumbEl.removeEventListener("mousedown", t.onDragStart), t.barContainer && (t.barContainer.removeEventListener("click", t.onBarClick), t.barContainer.remove()), t.bottomIsVisibleObserverEl && t.bottomIsVisibleObserverEl.remove(), delete e.__tableScrollData);
167
+ }, H = {
168
+ mounted(e, t) {
169
+ setTimeout(() => {
170
+ M(e, t);
171
+ }, 150);
172
+ },
173
+ updated(e, t) {
174
+ setTimeout(() => {
175
+ const o = e.__tableScrollData;
176
+ o ? (o.mode = t.value === "always" ? "always" : "hover", o.updatePosition()) : M(e, t);
177
+ }, 150);
178
+ },
179
+ unmounted(e) {
180
+ V(e);
181
+ }
182
+ }, B = (e) => {
183
+ e.directive("table-horizontal-scroll", H);
184
+ }, P = {
185
+ install: B
186
+ };
187
+ export {
188
+ P as default,
189
+ H as tableHorizontalScrollDirective
190
+ };
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@leslie1900/el-table-horizontal-scroll",
3
+ "version": "1.0.0",
4
+ "description": "A Vue 3 directive to optimize horizontal scrollbar for Element Plus table.",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "vite build",
20
+ "prepublishOnly": "npm run build"
21
+ },
22
+ "keywords": [
23
+ "vue",
24
+ "vue3",
25
+ "element-plus",
26
+ "el-table",
27
+ "scrollbar",
28
+ "horizontal-scroll",
29
+ "directive"
30
+ ],
31
+ "author": "",
32
+ "license": "MIT",
33
+ "peerDependencies": {
34
+ "vue": "^3.0.0"
35
+ },
36
+ "devDependencies": {
37
+ "vue": "^3.5.17",
38
+ "vite": "^4.4.8",
39
+ "typescript": "^4.7.4",
40
+ "vite-plugin-dts": "^3.5.0"
41
+ }
42
+ }