Vue3中滚动条在容器中始终滚动到底部

<template>
<section class="list-remark-box" v-if="props.feedbackData.length > 0" ref="listRef">
// .......
</section>
</template>
<script setup>
const listRef = ref(null)
const onScrollChange = () => {
setTimeout(() => {
listRef.value.scrollTop = listRef.value.scrollHeight
})
}
watch(() => props.feedbackData, () => {
if (props.feedbackData.length > 0) {
onScrollChange()
}
}, { deep: true })
</script>

verion 1.0.1

<script setup>
import { ref, reactive, onMounted, defineProps, nextTick } from 'vue'
const scrollDiv = ref(null)
onMounted(() => {
//划动回到顶部
nextTick(() => {
let scrollElem = scrollDiv.value;
scrollElem.scrollTo({ top: 0, behavior: 'smooth' });
});
// 滚动到底部
nextTick(() => {
let scrollElem = scrollDiv.value;
scrollElem.scrollTo({ top: scrollElem.scrollHeight, behavior: 'smooth' });
});
})
</script>
posted @   Felix_Openmind  阅读(1724)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2022-05-20 通过Lambda表达式对List进行分组查询并统计相关数据参数
2022-05-20 微服务简介
2022-05-20 使用Hutool的ExcelWriter实现Excel循环主次标题导出
*{cursor: url(https://files-cdn.cnblogs.com/files/morango/fish-cursor.ico),auto;}
点击右上角即可分享
微信分享提示