【问题标题】:Scroll page to div onload on vue js滚动页面以在 vue js 上加载 div
【发布时间】:2020-02-29 10:18:21
【问题描述】:
我在使用 vue js 在页面加载时将页面滚动到 div 时遇到问题。
<div v-for="(answer, index) in answers">
<p>{{answer.id}}</p> <p>{{answer.body}}</p>
</div>
如何滚动页面加载以回答具有特定 ID 的问题?
【问题讨论】:
标签:
javascript
vue.js
scroll
vue-component
vuex
【解决方案1】:
尝试为每个div 添加一个ref,例如:
<div v-for="(answer, index) in answers" :ref="answer.id">
<p>{{answer.id}}</p> <p>{{answer.body}}</p>
</div>
并在挂载的钩子中添加以下代码:
mounted(){
this.$refs["4"].scrollIntoView({behavior: "smooth", block: "start", inline: "start"});
//replace 4 by your preferred id
}