{
- homeStore.sortField.value = sortField
+const handleSortByField = (field: string) => {
+ useKUNGalgameHomeStore().resetPageStatus()
+ sortField.value = field
}
const orderAscending = () => {
- homeStore.sortOrder.value = 'asc'
+ useKUNGalgameHomeStore().resetPageStatus()
+ sortOrder.value = 'asc'
}
const orderDescending = () => {
- homeStore.sortOrder.value = 'desc'
+ useKUNGalgameHomeStore().resetPageStatus()
+ sortOrder.value = 'desc'
}
diff --git a/src/views/topic/KUNGalgameTopicPage.vue b/src/views/topic/KUNGalgameTopicPage.vue
index 9561e6b6..7ef1bd8e 100644
--- a/src/views/topic/KUNGalgameTopicPage.vue
+++ b/src/views/topic/KUNGalgameTopicPage.vue
@@ -33,14 +33,9 @@ import { storeToRefs } from 'pinia'
// 当前的路由
const route = useRoute()
-// 使用设置面板的 store
-const settingsStore = useKUNGalgameSettingsStore()
-// 使用话题页面的 store
-const topicStore = useKUNGalgameTopicStore()
-
-const { showKUNGalgamePageWidth } = storeToRefs(settingsStore)
-const { isShowAdvance, isEdit, replyDraft, replyRequest } =
- storeToRefs(topicStore)
+const { showKUNGalgamePageWidth } = storeToRefs(useKUNGalgameSettingsStore())
+const { isShowAdvance, isEdit, replyDraft, replyRequest, isScrollToTop } =
+ storeToRefs(useKUNGalgameTopicStore())
const tid = computed(() => {
return Number(route.params.tid)
@@ -82,6 +77,19 @@ watch(
{ immediate: true }
)
+// 监视是否滚动到顶部
+watch(isScrollToTop, () => {
+ if (content.value) {
+ // 将页面滚动到顶部
+ content.value.scrollTo({
+ top: 0,
+ behavior: 'smooth',
+ })
+ // 讲滚动值还原
+ isScrollToTop.value = false
+ }
+})
+
// 滚动事件处理函数
const scrollHandler = async () => {
// 滚动到底部的处理逻辑
@@ -112,7 +120,9 @@ const isScrollAtBottom = () => {
const scrollTop = content.value.scrollTop
const clientHeight = content.value.clientHeight
- return scrollHeight - scrollTop === clientHeight
+ // 使用误差范围来比较,因为 js 浮点数不精确
+ const errorMargin = 1.007
+ return Math.abs(scrollHeight - scrollTop - clientHeight) < errorMargin
}
}
@@ -134,6 +144,7 @@ onBeforeUnmount(() => {
element.removeEventListener('scroll', scrollHandler)
}
})
+
/* 话题界面的页面宽度 */
const topicPageWidth = computed(() => {
return showKUNGalgamePageWidth.value.Topic + '%'
@@ -145,10 +156,12 @@ const resetPanelStatus = () => {
isEdit.value = false
}
+// 页面离开时关闭回复面板
onBeforeRouteLeave(() => {
resetPanelStatus()
})
+// 取消挂载时关闭回复面板
onBeforeMount(() => {
resetPanelStatus()
})
diff --git a/src/views/topic/aside/components/AsideBase.vue b/src/views/topic/aside/components/AsideBase.vue
index 81d8f305..851298ad 100644
--- a/src/views/topic/aside/components/AsideBase.vue
+++ b/src/views/topic/aside/components/AsideBase.vue
@@ -7,7 +7,7 @@ import { asideNavItem } from './asideNavItem'
import { useKUNGalgameTopicStore } from '@/store/modules/topic'
import { storeToRefs } from 'pinia'
-const { replyRequest } = storeToRefs(useKUNGalgameTopicStore())
+const { isScrollToTop, replyRequest } = storeToRefs(useKUNGalgameTopicStore())
// 排序
const handleSortReply = (sortField: string) => {
@@ -26,11 +26,7 @@ const orderDescending = () => {
// 回到顶端
const handleBackToTop = () => {
- // 使用 window.scrollTo 方法将页面滚动到顶部
- window.scrollTo({
- top: 0, // 滚动到顶部的位置
- behavior: 'smooth', // 平滑滚动效果
- })
+ isScrollToTop.value = true
}
diff --git a/src/views/topic/aside/components/TopicAsideNav.vue b/src/views/topic/aside/components/TopicAsideNav.vue
index b6d69bcd..cce88e92 100644
--- a/src/views/topic/aside/components/TopicAsideNav.vue
+++ b/src/views/topic/aside/components/TopicAsideNav.vue
@@ -9,7 +9,7 @@ import { asideNavItem } from './asideNavItem'
import { useKUNGalgameTopicStore } from '@/store/modules/topic'
import { storeToRefs } from 'pinia'
-const { replyRequest } = storeToRefs(useKUNGalgameTopicStore())
+const { isScrollToTop, replyRequest } = storeToRefs(useKUNGalgameTopicStore())
const handleSortReply = (sortField: string) => {
replyRequest.value.sortField = sortField
@@ -24,11 +24,7 @@ const orderDescending = () => {
}
const handleBackToTop = () => {
- // 使用 window.scrollTo 方法将页面滚动到顶部
- window.scrollTo({
- top: 0, // 滚动到顶部的位置
- behavior: 'smooth', // 平滑滚动效果
- })
+ isScrollToTop.value = true
}