1
1
<script setup>
2
- const { tm , locale } = useI18n ();
2
+ const { tm , _locale } = useI18n ();
3
3
4
4
const getComp = computed (() => {
5
5
const textValue = tm (' BarLeft' );
@@ -75,6 +75,8 @@ const rowHeight = 32;
75
75
const chunkPading = 6 ;
76
76
const rowHeightpx = ` ${ rowHeight} px` ;
77
77
78
+ const thisElSubEnum = ref (undefined );
79
+
78
80
const route = useRoute ();
79
81
80
82
const openMenu = (MenuOpenEvent ) => {
@@ -87,67 +89,75 @@ const openMenu = (MenuOpenEvent) => {
87
89
break ;
88
90
} else {
89
91
height -=
90
- navigationList .value [item].children .length * rowHeight - chunkPading;
92
+ navigationList .value [item].children .length * rowHeight + chunkPading;
91
93
closeMenu (item);
92
94
}
93
95
}
94
96
openMenuList .add (MenuOpenEvent);
95
97
};
96
98
97
- const deleteSetItem = (target , setList ) => {
98
- for (const item of setList) {
99
- if (item === target) {
100
- setList .delete (item);
101
- break ;
102
- }
103
- }
104
- };
105
-
106
99
const closeMenu = (MenuOpenEvent ) => {
107
- deleteSetItem (MenuOpenEvent, openMenuList );
100
+ openMenuList . delete (MenuOpenEvent);
108
101
menuRef .value .close (MenuOpenEvent);
109
102
};
110
103
111
- const highlyIsQualified = (height ) => {
104
+ const highlyIsQualified = (
105
+ height,
106
+ mainHeight = menuDivRef .value .parentNode .parentNode .nextElementSibling
107
+ .clientHeight
108
+ ) => {
112
109
// 在中心内容长度小于window.innerHeight时,回到首页弹窗不可能出来
113
110
// 此时高度比较参照中心内容长度即可,不需要算上弹窗和底栏
114
111
// 中心内容长度大于window.innerHeight时,总长度要加上底栏和弹窗和1px的弹窗下边距与window.innerHeight对比
115
- if (
116
- height + rowHeight * 2 + 1 < window .innerHeight &&
117
- height <
118
- menuDivRef .value .parentNode .parentNode .nextElementSibling .clientHeight
119
- )
120
- return true ;
112
+ if (height < mainHeight) return true ;
121
113
return false ;
122
114
};
123
115
124
116
const { $mitt } = useNuxtApp ();
125
117
onMounted (() => {
126
- $mitt .on (' routeSwitching' , () => {
127
- nextTick (() => {
128
- retractMenuBar ();
129
- });
118
+ $mitt .on (' mainDomChange' , (newHeight ) => {
119
+ retractMenuBar (newHeight);
130
120
});
131
121
});
132
122
onBeforeUnmount (() => {
133
- $mitt .off (' routeSwitching ' );
123
+ $mitt .off (' mainDomChange ' );
134
124
});
135
125
136
- const retractMenuBar = () => {
126
+ const changeThisElSubEnum = () => {
127
+ let thisElSubEnmu_cache = undefined ;
128
+ if (route .path !== ' /' ) {
129
+ for (const [index , item ] of navigationList .value .entries ()) {
130
+ if (item .children .find ((item1 ) => route .path .includes (item1 .url ))) {
131
+ // 记录当前所在
132
+ thisElSubEnmu_cache = index;
133
+ break ;
134
+ }
135
+ }
136
+ if (thisElSubEnum .value !== undefined )
137
+ openMenuList .add (String (thisElSubEnum .value ));
138
+
139
+ openMenuList .delete (String (thisElSubEnmu_cache));
140
+ thisElSubEnum .value = thisElSubEnmu_cache;
141
+ }
142
+ };
143
+
144
+ const autoFold = (newHeight ) => {
137
145
let height = menuDivRef .value .clientHeight ;
138
146
for (const item of openMenuList) {
139
- if (highlyIsQualified (height)) {
147
+ if (highlyIsQualified (height, newHeight )) {
140
148
break ;
141
149
} else {
142
150
if (openMenuList .size === 1 ) break ;
143
151
height -=
144
- navigationList .value [item].children .length * rowHeight - chunkPading;
152
+ navigationList .value [item].children .length * rowHeight + chunkPading;
145
153
closeMenu (item);
146
154
}
147
155
}
148
- // for (const item of openMenuList) {
149
- // menuRef.value.open(item);
150
- // }
156
+ };
157
+
158
+ const retractMenuBar = (newHeight ) => {
159
+ changeThisElSubEnum (newHeight);
160
+ autoFold (newHeight);
151
161
};
152
162
onMounted (() => {
153
163
// 每次缩放改变的时候,判断有没有栏目需要缩回去,先展开的,优先缩进
@@ -156,42 +166,32 @@ onMounted(() => {
156
166
return () => {
157
167
if (timeoutID !== undefined ) clearTimeout (timeoutID);
158
168
timeoutID = setTimeout (() => {
159
- retractMenuBar ();
169
+ autoFold ();
160
170
timeoutID = undefined ;
161
171
}, 40 );
162
172
};
163
173
})();
164
- let height = menuDivRef .value .clientHeight ;
165
- // 初次加载的时候尝试打开当前栏目分类
166
- // 记一下目前所在分类的title
167
- let thisElSubEnum = null ;
168
- for (const [index , item ] of navigationList .value .entries ()) {
169
- if (thisElSubEnum === null ) {
170
- if (item .children .find ((item1 ) => route .path .includes (item1 .url ))) {
171
- // 当前所在不入列尾
172
- thisElSubEnum = index;
173
- } else {
174
- openMenuList .add (String (index));
175
- }
176
- } else {
177
- openMenuList .add (String (index));
178
- }
179
- }
180
- // 然后在剩余空间里按顺序遍历栏目,能展开尽量展开
181
- // 默认全部展开,根据空间从下向上依次关闭,跳过当前栏目
182
- for (const [index , item ] of reverseIterator (navigationList .value )) {
183
- if (highlyIsQualified (height)) {
184
- break ;
185
- }
186
- if (thisElSubEnum !== index) {
187
- height = height - (item .children .length * rowHeight + chunkPading);
188
- closeMenu (String (index));
189
- }
174
+ // 默认全部展开,找到并记一下目前所在栏目
175
+ for (let i = 0 ; i < navigationList .value .length ; i++ ) {
176
+ openMenuList .add (String (i));
190
177
}
191
178
// 判断当前所在位置是否需要回到顶部按钮
192
- returnFromTop ();
193
- // 挂载上面监听器
179
+ // 挂载监听器
194
180
window .addEventListener (' scroll' , returnFromTop);
181
+
182
+ // 当前会触发一次retractMenuBar无需手动处理
183
+ // // 然后在剩余空间里按顺序遍历栏目,能展开尽量展开
184
+ // // 默认全部展开,根据空间从下向上依次关闭,跳过当前栏目
185
+ // let height = menuDivRef.value.clientHeight;
186
+ // for (const [index, item] of reverseIterator(navigationList.value)) {
187
+ // if (highlyIsQualified(height)) {
188
+ // break;
189
+ // }
190
+ // if (thisElSubEnum.value !== index) {
191
+ // height -= item.children.length * rowHeight + chunkPading;
192
+ // closeMenu(String(index));
193
+ // }
194
+ // }
195
195
});
196
196
197
197
const returnFromTop = (() => {
@@ -234,6 +234,7 @@ const backToTopBtnShow = ref(false);
234
234
:default-openeds =" defaultOpeneds"
235
235
@close =" closeMenu"
236
236
@open =" openMenu" >
237
+ <!-- 它的 index 要求是字符串不然控制台发警告 -->
237
238
<el-sub-menu
238
239
v-for =" (item, index) in navigationList"
239
240
:key =" `barleft-1-link-${index}`"
0 commit comments