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

pull/137/head
dhb52 2023-04-22 23:45:14 +08:00
parent 198752868c
commit cb4527748e
1 changed files with 12 additions and 18 deletions

View File

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