在一个区域会依次打印log,随着log的加长,出现滚动条,而滚动条应该始终保持在最下方。
点击回到顶部按钮,可以使滚动条回到最上方
// template
<div class="bottom-logs" ref="logRef">
// antd vue中的back-top组件
<a-back-top :target="() => logRef" :visibilityHeight="10" @click="handleBackToTop" />
<LogData :logData="log"></LogData>
</div>
// css
// 父级元素 display: flex
.bottom-logs {
flex: 1;
overflow: auto;
}
<script setup>
import { ref, watch, nextTick } from 'vue'
const logRef = ref();
// log为响应式数组
watch(log, () => {
nextTick(() => {
const content = logRef.value;
content.scrollTo({ top: content.scrollHeight, behavior: "smooth" });
});
}, {
deep: true,
});
const handleBackToTop = () => {
const content = logRef.value;
content.scrollTo({ top: 0, behavior: "smooth" });
}
</script>