vue多Tab栏切换点击左右滑动

最近看到一个项目上使用的 Tan 切换很有意思,可以左右点击控制 tab 按钮的滑动。后来发现是用的 layui admin 实现的。

先看效果图

效果图

这里是 vue3 + element-plus

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<template>
<nav class="nav-container">
<span
class="indicate-btn"
@click="handleSollPage('left')">
<i class="el-icon-d-arrow-left"></i>
</span>
<el-tag
key="首页"
:effect="'/home' === $route.path ? 'dark' : 'plain'"
@click="handleTo({ path: '/home' })">
<i class="el-icon-s-home" />
首页
</el-tag>
<div class="sliding-window">
<div
class="sliding-container"
:style="{ transform: `translateX(${offsetX}px)` }">
<el-tag
v-for="(tag, i) in tabsList"
:key="`${tag.name}-${tag.path}`"
closable
:effect="tag.path === $route.path ? 'dark' : 'plain'"
@close="handleClose(tag, i)"
@click="handleTo(tag)">
<i
:data-index="i"
:class="tag.icon" />
{{ tag.name }}
</el-tag>
</div>
</div>
<span
class="indicate-btn"
@click="handleSollPage('right')">
<i class="el-icon-d-arrow-right"></i>
</span>
</nav>
</template>
<script lang="ts">
import { useStore } from 'vuex';
import { defineComponent, ref, toRefs, unref } from 'vue';
import { RouteLocationRaw, useRouter } from 'vue-router';
import { Menu } from '@/custom';
import { key } from '@/store';

export default defineComponent({
name: 'Crumbs',

setup() {
const store = useStore(key);
const { currentMenu, tabsList } = toRefs(store.state.crumbs);
const router = useRouter();
const firstSubscript = ref(0);
const offsetX = ref(0);
const handSwitchTab = (route: Menu) =>
store.commit('crumbs/switchTab', route);
const handRemoveTab = (route: Menu) =>
store.commit('crumbs/removeTab', route);

const handleTo = (route: Menu) => {
handSwitchTab(route);
router.push(route.path as RouteLocationRaw);
};
const handleClose = (route: Menu, index: number) => {
if (unref(currentMenu).path === route.path) {
if (index === 0 && tabsList && unref(tabsList).length === 1) {
router.push('/home');
} else {
if (index !== unref(tabsList).length - 1) {
router.push(unref(tabsList)[index + 1].path as RouteLocationRaw);
} else {
router.push(unref(tabsList)[index - 1].path as RouteLocationRaw);
}
}
}
handRemoveTab(route);
};
/* 实现部分 */
const handleSollPage = (direction: string) => {
const tagsNode: Element[] = Array.from(
document.querySelectorAll('.sliding-container>span.el-tag')
);
const windowNode = document.querySelector('.sliding-container');
/* margin + border 宽度 */
const marginBorder = 6;

const getTagsNodeWidht = (start: number, end: number): number => {
let l = 0;
for (
let i = start;
i <= end && start < tagsNode.length && end < tagsNode.length;
i++
) {
const el = tagsNode[i];
l = l + el.clientWidth + marginBorder;
}
return l;
};
const totaLength = getTagsNodeWidht(0, tagsNode.length - 1);
if ('left' === direction) {
for (let i = firstSubscript.value; i < tagsNode.length; i++) {
if (
windowNode &&
getTagsNodeWidht(firstSubscript.value, i) > windowNode.clientWidth
) {
const _offsetX =
offsetX.value - getTagsNodeWidht(firstSubscript.value, i - 1);

if (Math.abs(totaLength) > Math.abs(_offsetX)) {
firstSubscript.value = i;
offsetX.value = -1 * getTagsNodeWidht(0, i - 1);
}
return false;
}
}
}
if ('right' === direction) {
for (let i = -1 + firstSubscript.value; i >= 0; i--) {
if (
windowNode &&
getTagsNodeWidht(i, firstSubscript.value - 1) >
windowNode.clientWidth
) {
const _offsetX =
offsetX.value + getTagsNodeWidht(i, firstSubscript.value - 1);
if (Math.abs(totaLength) > Math.abs(_offsetX) && _offsetX <= 0) {
firstSubscript.value = i;
offsetX.value = -1 * getTagsNodeWidht(0, i);
}
return false;
} else if (
windowNode &&
getTagsNodeWidht(0, firstSubscript.value - 1) <=
windowNode.clientWidth
) {
firstSubscript.value = 0;
offsetX.value = 0;
return false;
}
}
}
};

return {
currentMenu,
tabsList,
offsetX,
handleTo,
handleClose,
handleSollPage,
};
},
});
</script>
<style
lang="scss"
scoped>
.nav-container {
display: flex;
padding: 0.03rem 0.05rem;
border-bottom: 1px solid #999;
.indicate-btn {
display: inline-block;
width: 0.5rem;
min-width: 0.3rem;
line-height: 30px;
text-align: center;
cursor: pointer;
border-radius: 2px;
border: 1px solid rgb(211, 211, 211);
&:hover {
background-color: rgba(0, 0, 0, 0.1);
}
}
.sliding-window {
position: relative;
flex-grow: 1;
overflow: hidden;
.sliding-container {
position: relative;
display: flex;
transition: transform 0.3s;
}
}
}
:deep(.el-tag) {
cursor: pointer;
margin: 0 2px;
}
</style>

作为储备库 待用 ~~~

-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!
0%