scroll - Nuxt.js 滚动到 div 元素的底部
问题描述
这段代码有什么问题?渲染 div 元素后,滚动必须在 div 元素的底部。scrollTop 在 func 之后不保存值
<main>
<div v-if="messages">
<ul ref="messagesContainer">
<li v-for="message in messages" :key="`${message.text}.${message.username}`">
<Message :message="message" :username="username" />
</li>
</ul>
</div>
</main>
mounted() {
this.scrollToEnd();
},
methods: {
scrollToEnd() {
const height = this.$refs.messagesContainer.scrollHeight;
this.$refs.messagesContainer.scrollTo(0,height)
// doesnt work
// this.$refs.messagesContainer.scrollTop = height
}
}
main {
background-color: #fff;
height: 56vh;
ul {
height: 100%;
overflow: auto;
flex: auto;
}
}
解决方案
offsetHeight
您可以通过如下属性
获取元素的 y 位置:var offsetTop = this.$refs.messagesContainer.offsetHeight;
您可以在此处找到有关offsetHeight
,clientHeight
和scrollHeight
属性的更多信息
请注意,您需要在元素上调用该scrollTo
方法:window
例子:
methods: {
scrollToEnd() {
var scrollHeight = this.$refs.messagesContainer.offsetHeight;
this.$refs.messagesContainer.scrollTop = scrollHeight;
}
}
希望这可以帮助 :)
推荐阅读
- java - 如何使用 KStream 中的值分配新变量
- python-3.x - 我们如何在 jupyter lab 中配置默认导入我已经在 jupyter notebook 中完成了这项工作,但它在 jupyter lab notebook 中失败
- mongodb - MongoDB按数组中的指定元素对文档进行排序
- xml - 使用 Zeep 问题的工作日 SOAP API
- python - 为什么我们在所有类方法上都传递 self ?
- excel - 有没有办法根据另一列中的一个单元格扫描整列并提取相应列的值?
- python - 如何在python的多个方法中使用var
- regex - 正则表达式:从字符串末尾开始匹配模式
- r - 对 glmer 模型的估计进行计算并在图中使用结果
- android - Android 多行纯文本背景