@@ -61,6 +61,7 @@ const openMenuList = new Set();
6161const menuDivRef = useTemplateRef (' menuDiv' );
6262const menuRef = useTemplateRef (' menu' );
6363const rowHeight = 32 ;
64+ const chunkPading = 6 ;
6465const rowHeightpx = ` ${ rowHeight} px` ;
6566
6667const 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+
8990const 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
9399const 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
0 commit comments