@@ -61,6 +61,7 @@ const openMenuList = new Set();
61
61
const menuDivRef = useTemplateRef (' menuDiv' );
62
62
const menuRef = useTemplateRef (' menu' );
63
63
const rowHeight = 32 ;
64
+ const chunkPading = 6 ;
64
65
const rowHeightpx = ` ${ rowHeight} px` ;
65
66
66
67
const route = useRoute ();
@@ -70,24 +71,29 @@ const openMenu = (MenuOpenEvent) => {
70
71
(item ) => item .title === MenuOpenEvent
71
72
);
72
73
let height =
73
- result .children .length * rowHeight + menuDivRef .value .clientHeight ;
74
+ result .children .length * rowHeight +
75
+ chunkPading +
76
+ menuDivRef .value .clientHeight ;
74
77
for (const item of openMenuList) {
75
78
if (highlyIsQualified (height)) {
76
79
break ;
77
80
} 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;
83
82
openMenuList .delete (item);
84
- menuRef .value .close (item);
83
+ console .log (item[0 ]);
84
+ menuRef .value .close (item[0 ]);
85
85
}
86
86
}
87
- openMenuList .add (MenuOpenEvent);
87
+ openMenuList .add ([ MenuOpenEvent, result . children . length ] );
88
88
};
89
+
89
90
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
+ }
91
97
};
92
98
93
99
const highlyIsQualified = (height ) => {
@@ -146,36 +152,31 @@ onMounted(() => {
146
152
// 初次加载的时候尝试打开当前栏目分类
147
153
// 记一下目前所在分类的title
148
154
let thisTitle = null ;
149
- let thisColumnIsShow = false ;
150
155
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 )
153
158
);
154
159
if (resule) {
155
160
height = height + item .children .length * rowHeight;
156
- if (highlyIsQualified (height)) {
157
- // 记下可以展开,但先不展开,因为要给这个放到队列尾
158
- thisColumnIsShow = true ;
159
- }
161
+ // 展开但不进入队列,因为要给这个放到队列尾
160
162
thisTitle = item .title ;
161
163
break ;
162
164
}
163
165
}
164
166
// 然后在剩余空间里按顺序遍历栏目,能展开尽量展开
165
167
for (const item of navigationList .value ) {
166
168
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 ;
170
172
menuRef .value .open (item .title );
171
173
} else {
172
174
break ;
173
175
}
174
176
}
175
177
}
176
178
// 剩余栏目展开完毕,展开当前所在栏目,此时屏幕缩小优先关闭其他栏目
177
- if (thisColumnIsShow) {
178
- openMenuList .add (thisTitle);
179
+ if (thisTitle !== null ) {
179
180
menuRef .value .open (thisTitle);
180
181
}
181
182
@@ -196,7 +197,7 @@ const returnFromTop = (() => {
196
197
backToTopBtnShow .value = false ;
197
198
}
198
199
timeoutID = undefined ;
199
- }, 20 );
200
+ }, 70 );
200
201
};
201
202
})();
202
203
0 commit comments