我正在与vue在这个项目上工作,有一个帖子列表,显示在用户主页上,当点击显示完整帖子时,我用单个帖子的扩展视图替换显示的帖子列表。
当用户返回查看帖子列表时,无论他们以前滚动到哪里,页面都会滚动回到顶部。
我希望页面转到他们在他们点击查看完整帖子之前所在的位置。
我所做的是在单击show full post时存储当前滚动位置的值,然后在单击back按钮时使用window.scrollTo转到该位置。
这在第一次是有效的,但对于后续的时间,它总是第一次到达该位置。
我认为这是vue缓存函数或其他方面的问题,但我不确定如何处理。
<template>
<div>
<post-list @showFullPost="showFullPost($event)" v-show="!showingFullPost"></post-list>
<full-post @goBack="showAllPosts" v-if="showingFullPost"></full-post>
</div>
</template>
<script>
export default{
data: function(){
return {
currentHeight: 0,
showingFullPost: false
}
},
methods:{
showFullPost(post){
this.currentHeight = window.scrollY;
//other code
},
showAllPosts(){
//some code
window.scrollTop(0, this.currentHeight); //this only works the first time and subsequently goes to the position of the first time
}
}
}
</script>发布于 2019-10-02 21:01:50
我找到了一个解决方案。
我不知道为什么会这样,但是使用setTimeout函数解决了这个问题。
setTimeout(() => {
window.scrollTop(0, this.currentHeight);
}, 20);https://stackoverflow.com/questions/58194006
复制相似问题