-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathCategoryNavigation.vue
More file actions
188 lines (167 loc) · 4.25 KB
/
CategoryNavigation.vue
File metadata and controls
188 lines (167 loc) · 4.25 KB
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
<template>
<div class="category-nav-wrapper" ref="navWrapperRef">
<!-- 分类tabs -->
<a-tabs
v-model:active-key="activeKey"
class="category-tabs"
type="line"
size="large"
position="top"
:editable="false"
@change="handleTabChange"
>
<a-tab-pane v-if="hasRecommendVideos" key="recommendTuijian404" title="推荐" />
<a-tab-pane
v-for="item in classList?.class || []"
:key="item.type_id"
:title="item.type_name"
/>
</a-tabs>
<!-- 分类管理按钮 -->
<div class="category-manage" @click="openCategoryModal">
<icon-apps />
</div>
</div>
</template>
<script setup>
import { ref, computed, watch, onMounted, onBeforeUnmount } from 'vue';
import { IconApps } from '@arco-design/web-vue/es/icon';
const props = defineProps({
classList: {
type: Object,
default: () => ({})
},
trigger: {
type: String,
default: 'click'
},
hasRecommendVideos: {
type: Boolean,
default: false
},
activeKey: {
type: String,
default: ''
}
});
const emit = defineEmits(['tab-change', 'open-category-modal']);
// 计算默认的activeKey
const getDefaultActiveKey = () => {
if (props.hasRecommendVideos) {
return "recommendTuijian404";
}
if (props.classList?.class && props.classList.class.length > 0) {
return props.classList.class[0].type_id;
}
return "recommendTuijian404";
};
// 响应式数据
const activeKey = ref(props.activeKey || getDefaultActiveKey());
const navWrapperRef = ref(null);
let wheelHandler = null;
// 监听props变化,更新activeKey
watch(() => [props.hasRecommendVideos, props.classList, props.activeKey], () => {
const newActiveKey = props.activeKey || getDefaultActiveKey();
if (activeKey.value !== newActiveKey) {
activeKey.value = newActiveKey;
// 只有当activeKey不是从外部传入时才触发change事件
if (!props.activeKey) {
handleTabChange(newActiveKey);
}
}
}, { immediate: true });
const handleTabChange = (key) => {
activeKey.value = key;
emit('tab-change', key);
};
const openCategoryModal = () => {
emit('open-category-modal');
};
onMounted(() => {
const wrapper = navWrapperRef.value;
if (!wrapper) return;
const list = wrapper.querySelector('.arco-tabs-nav-tab-list');
if (!list) return;
// 将垂直滚轮转换为水平滚动,提升可用性
wheelHandler = (ev) => {
// 仅当垂直滚动明显时处理
if (Math.abs(ev.deltaY) > Math.abs(ev.deltaX)) {
list.scrollLeft += ev.deltaY;
ev.preventDefault();
}
};
list.addEventListener('wheel', wheelHandler, { passive: false });
});
onBeforeUnmount(() => {
const wrapper = navWrapperRef.value;
const list = wrapper?.querySelector?.('.arco-tabs-nav-tab-list');
if (list && wheelHandler) {
list.removeEventListener('wheel', wheelHandler);
}
});
</script>
<style scoped>
.category-nav-wrapper {
display: flex;
align-items: center;
padding: 0 16px;
background: white;
border-bottom: 1px solid #f0f0f0;
}
.category-tabs {
flex: 1;
overflow: hidden;
min-width: 0; /* 允许在flex布局中收缩 */
}
/* 确保tabs的高度与管理按钮对齐 */
.category-tabs :deep(.arco-tabs-nav-wrap) {
height: 40px;
display: flex;
align-items: center;
}
.category-tabs :deep(.arco-tabs-tab) {
height: 40px;
display: flex;
align-items: center;
}
/* 启用tabs横向滚动 */
.category-tabs :deep(.arco-tabs-nav) {
overflow: visible; /* 允许内部列表展示溢出部分 */
}
.category-tabs :deep(.arco-tabs-nav-tab-list) {
overflow-x: auto;
overflow-y: hidden;
scrollbar-width: none;
-ms-overflow-style: none;
scroll-behavior: smooth;
display: flex;
flex-wrap: nowrap;
white-space: nowrap;
}
.category-tabs :deep(.arco-tabs-nav-tab-list)::-webkit-scrollbar {
display: none;
}
.category-tabs :deep(.arco-tabs-tab) {
flex-shrink: 0;
white-space: nowrap;
min-width: auto;
}
.category-manage {
display: flex;
align-items: center;
justify-content: center;
width: 40px;
height: 40px;
border-radius: 8px;
background: #f5f5f5;
cursor: pointer;
transition: all 0.2s;
margin-left: 16px;
flex-shrink: 0;
transform: translateY(-6px);
}
.category-manage:hover {
background: #e6f7ff;
color: #1890ff;
}
</style>