hookehuyr

feat(StudyDetailPage): 添加滚动监听以更新活动标签

在StudyDetailPage组件中添加滚动监听,根据滚动位置动态更新活动标签。同时,在组件卸载时移除滚动监听以避免内存泄漏。
...@@ -202,6 +202,22 @@ useTitle('学习详情'); ...@@ -202,6 +202,22 @@ useTitle('学习详情');
202 const topWrapperHeight = ref(0); 202 const topWrapperHeight = ref(0);
203 const bottomWrapperHeight = ref(0); 203 const bottomWrapperHeight = ref(0);
204 204
205 +const handleScroll = () => {
206 + const introElement = document.getElementById('intro');
207 + const commentElement = document.getElementById('comment');
208 + if (!introElement || !commentElement) return;
209 +
210 + const scrollTop = window.scrollY;
211 + const commentOffset = commentElement.offsetTop - parseInt(topWrapperHeight.value);
212 +
213 + // 根据滚动位置更新activeTab
214 + if (scrollTop >= commentOffset) {
215 + activeTab.value = 'comments';
216 + } else {
217 + activeTab.value = 'intro';
218 + }
219 +};
220 +
205 onMounted(() => { 221 onMounted(() => {
206 nextTick(() => { 222 nextTick(() => {
207 const topWrapper = document.querySelector('.top-wrapper'); 223 const topWrapper = document.querySelector('.top-wrapper');
...@@ -212,6 +228,9 @@ onMounted(() => { ...@@ -212,6 +228,9 @@ onMounted(() => {
212 if (bottomWrapper) { 228 if (bottomWrapper) {
213 bottomWrapperHeight.value = bottomWrapper.clientHeight + 'px'; 229 bottomWrapperHeight.value = bottomWrapper.clientHeight + 'px';
214 } 230 }
231 +
232 + // 添加滚动监听
233 + window.addEventListener('scroll', handleScroll);
215 }) 234 })
216 const courseId = route.params.id; 235 const courseId = route.params.id;
217 if (courseId) { 236 if (courseId) {
...@@ -269,6 +288,11 @@ const handleTabChange = (name) => { ...@@ -269,6 +288,11 @@ const handleTabChange = (name) => {
269 }; 288 };
270 289
271 290
291 +// 在组件卸载时移除滚动监听
292 +onUnmounted(() => {
293 + window.removeEventListener('scroll', handleScroll);
294 +});
295 +
272 </script> 296 </script>
273 297
274 <style lang="less" scoped> 298 <style lang="less" scoped>
......