Skip to content

Commit 8c776e9

Browse files
committed
fix: 修复侧栏自动展开 bug
1 parent 0b84738 commit 8c776e9

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

components/bar/BarLeft.vue

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const openMenuList = new Set();
6161
const menuDivRef = useTemplateRef('menuDiv');
6262
const menuRef = useTemplateRef('menu');
6363
const rowHeight = 32;
64+
const chunkPading = 6;
6465
const rowHeightpx = `${rowHeight}px`;
6566
6667
const route = useRoute();
@@ -70,24 +71,29 @@ const openMenu = (MenuOpenEvent) => {
7071
(item) => item.title === MenuOpenEvent
7172
);
7273
let height =
73-
result.children.length * rowHeight + menuDivRef.value.clientHeight;
74+
result.children.length * rowHeight +
75+
chunkPading +
76+
menuDivRef.value.clientHeight;
7477
for (const item of openMenuList) {
7578
if (highlyIsQualified(height)) {
7679
break;
7780
} else {
78-
height =
79-
height -
80-
navigationList.value.find((item1) => item1.title === item).children
81-
.length *
82-
rowHeight;
81+
height = height - item[1] * rowHeight - chunkPading;
8382
openMenuList.delete(item);
84-
menuRef.value.close(item);
83+
console.log(item[0]);
84+
menuRef.value.close(item[0]);
8585
}
8686
}
87-
openMenuList.add(MenuOpenEvent);
87+
openMenuList.add([MenuOpenEvent, result.children.length]);
8888
};
89+
8990
const closeMenu = (MenuOpenEvent) => {
90-
openMenuList.delete(MenuOpenEvent);
91+
for (const arr of openMenuList) {
92+
if (arr[0] === MenuOpenEvent) {
93+
openMenuList.delete(arr);
94+
break; // 删除后跳出循环,因为我们只删除第一个
95+
}
96+
}
9197
};
9298
9399
const highlyIsQualified = (height) => {
@@ -146,36 +152,31 @@ onMounted(() => {
146152
// 初次加载的时候尝试打开当前栏目分类
147153
// 记一下目前所在分类的title
148154
let thisTitle = null;
149-
let thisColumnIsShow = false;
150155
for (const item of navigationList.value.values()) {
151-
const resule = item.children.find(
152-
(item1) => item1.url === route.path.replace(/\/+$/, '')
156+
const resule = item.children.find((item1) =>
157+
route.path.includes(item1.url)
153158
);
154159
if (resule) {
155160
height = height + item.children.length * rowHeight;
156-
if (highlyIsQualified(height)) {
157-
// 记下可以展开,但先不展开,因为要给这个放到队列尾
158-
thisColumnIsShow = true;
159-
}
161+
// 展开但不进入队列,因为要给这个放到队列尾
160162
thisTitle = item.title;
161163
break;
162164
}
163165
}
164166
// 然后在剩余空间里按顺序遍历栏目,能展开尽量展开
165167
for (const item of navigationList.value) {
166168
if (thisTitle !== item.title) {
167-
height = height + item.children.length * rowHeight;
168-
if (highlyIsQualified(height)) {
169-
openMenuList.add(item.title);
169+
const cache_h = height + item.children.length * rowHeight + chunkPading;
170+
if (highlyIsQualified(cache_h)) {
171+
height = cache_h;
170172
menuRef.value.open(item.title);
171173
} else {
172174
break;
173175
}
174176
}
175177
}
176178
// 剩余栏目展开完毕,展开当前所在栏目,此时屏幕缩小优先关闭其他栏目
177-
if (thisColumnIsShow) {
178-
openMenuList.add(thisTitle);
179+
if (thisTitle !== null) {
179180
menuRef.value.open(thisTitle);
180181
}
181182
@@ -196,7 +197,7 @@ const returnFromTop = (() => {
196197
backToTopBtnShow.value = false;
197198
}
198199
timeoutID = undefined;
199-
}, 20);
200+
}, 70);
200201
};
201202
})();
202203

layouts/default.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<template>
22
<div class="min-h-[100vh] min-w-[960px]">
3-
<div class="my-0 flex min-h-[100vh] flex-col p-0">
3+
<div class="header_main my-0 flex flex-col p-0">
44
<BarHeader />
5-
<div class="main-content flex flex-1 justify-center pb-[2.5rem]">
5+
<div class="main-content flex flex-1 justify-center pb-[0.5rem]">
66
<div class="content-container myShadow flex">
77
<div
88
class="sticky top-0 w-[15%] min-w-[12rem] bg-leftbar-bg *:text-nowrap">
@@ -15,3 +15,8 @@
1515
<BarFooter class="fixed bottom-0 h-[2rem]" />
1616
</div>
1717
</template>
18+
<style lang="scss">
19+
.header_main {
20+
height: calc(100vh - 2rem);
21+
}
22+
</style>

0 commit comments

Comments
 (0)