refactor: mp/wx-msg 采用ref来实现滚动

This commit is contained in:
dhb52 2023-04-22 23:45:14 +08:00
parent 198752868c
commit cb4527748e

View File

@ -7,7 +7,7 @@
--> -->
<template> <template>
<ContentWrap> <ContentWrap>
<div class="msg-div" :id="msgDivId"> <div class="msg-div" ref="msgDivRef">
<!-- 加载更多 --> <!-- 加载更多 -->
<div v-loading="loading"></div> <div v-loading="loading"></div>
<div v-if="!loading"> <div v-if="!loading">
@ -47,8 +47,7 @@ const props = defineProps({
} }
}) })
const accountId = ref<number>(-1) // IDuserId const accountId = ref(-1) // IDuserId
const msgDivId = `msg-div-{new Date().getTime()}` // :id="'msg-div' + nowStr"
const loading = ref(false) // const loading = ref(false) //
const hasMore = ref(true) // const hasMore = ref(true) //
const list = ref<any[]>([]) // const list = ref<any[]>([]) //
@ -74,7 +73,8 @@ const reply = ref<Reply>({
articles: [] articles: []
}) })
const replySelectRef = ref<InstanceType<typeof WxReplySelect> | null>(null) const replySelectRef = ref<InstanceType<typeof WxReplySelect> | null>(null) // WxReplySelectref
const msgDivRef = ref() // ref
/** 完成加载 */ /** 完成加载 */
onMounted(async () => { onMounted(async () => {
@ -89,7 +89,7 @@ onMounted(async () => {
// //
const sendMsg = async () => { const sendMsg = async () => {
if (!reply) { if (!unref(reply)) {
return return
} }
// //
@ -117,7 +117,7 @@ const loadMore = () => {
getPage(queryParams, null) getPage(queryParams, null)
} }
const getPage = async (page: any, params: any) => { const getPage = async (page: any, params: any = null) => {
loading.value = true loading.value = true
let dataTemp = await getMessagePage( let dataTemp = await getMessagePage(
Object.assign( Object.assign(
@ -131,11 +131,7 @@ const getPage = async (page: any, params: any) => {
) )
) )
const msgDiv = document.getElementById(msgDivId) const scrollHeight = msgDivRef.value?.scrollHeight ?? 0
let scrollHeight = 0
if (msgDiv) {
scrollHeight = msgDiv.scrollHeight
}
// //
const data = dataTemp.list.reverse() const data = dataTemp.list.reverse()
list.value = [...data, ...list.value] list.value = [...data, ...list.value]
@ -153,24 +149,22 @@ const getPage = async (page: any, params: any) => {
// //
await nextTick() await nextTick()
if (scrollHeight !== 0) { if (scrollHeight !== 0) {
let div = document.getElementById(msgDivId) if (msgDivRef.value) {
if (div && msgDiv) { msgDivRef.value.scrollTop = msgDivRef.value.scrollHeight - scrollHeight - 100
msgDiv.scrollTop = div.scrollHeight - scrollHeight - 100
} }
} }
} }
} }
const refreshChange = () => { const refreshChange = () => {
getPage(queryParams, null) getPage(queryParams)
} }
/** 定位到消息底部 */ /** 定位到消息底部 */
const scrollToBottom = async () => { const scrollToBottom = async () => {
await nextTick() await nextTick()
let div = document.getElementById(msgDivId) if (msgDivRef.value) {
if (div) { msgDivRef.value.scrollTop = msgDivRef.value.scrollHeight
div.scrollTop = div.scrollHeight
} }
} }
</script> </script>