位置:- 正文

vue中记录滚动条位置(vue 滚动条)

编辑:rootadmin
vue中记录滚动条位置 1、方法一

推荐整理分享vue中记录滚动条位置(vue 滚动条),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue怎么拿到滚动条的位置,vue 滚动条,vue滚动到指定位置,有滚动效果,vue记录滚动条位置,vue 记录滚动位置,vue页面滚动事件,vue数据滚动显示,vue数据滚动显示,内容如对您有帮助,希望把文章链接给更多的朋友!

vue中记录滚动条位置(vue 滚动条)

在点击的时候记录滚动条位置,存入本地 再次进入该路由读取滚动跳位置

1、1 跳转时路由存入scroll如果要有多个页面,可以把名称也存进去,这里默认一个页面 methods: { go() { console.log(document.documentElement.scrollTop) localStorage.setItem("scroll", document.documentElement.scrollTop) this.$router.push({path: '/'}) } }复制代码1、2 读取scroll位置再次进入该路由时从本地录取scroll位置

利用scrollBehavior ,在router/index.js中,配置路由中的scrollBehaviors事件

const router = new VueRouter({ mode: 'hash', routes, scrollBehavior(to, from){ if(to.name==='fatherSlot'){ //name为路由名字 return {x:0, y: parseInt(localStorage.getItem('scroll'))}//读取本地的scroll } console.log("to",to,"from",from) }})复制代码2、方法二

利用vue的keep-alive , 使用缓存机制来记录滚动条位置 但是有个缺点,页面刷新后就不记录了,但是简单高效

2、1 配置需要缓存的路由缓存pageA,pageB,里面代码都一样,如演示图一样。pageA,pageC为组件的名字.

html

<keep-alive :include="['pageA','pageC']"> <router-view/> </keep-alive>复制代码2、2 在路由内记录滚动位置

javascript

data() { return { curScrollTop: 0 //记录滚动条位置对象 } }, //此钩子函数会反复触发,是keep-alive特有的钩子函数,取 activated() { document.documentElement.scrollTop = this.curScrollTop || 0 }, //路由离开时的钩子函数,存 beforeRouteLeave (to, from, next) { this.curScrollTop = document.documentElement.scrollTop || 0 next() },
本文链接地址:https://www.jiuchutong.com/zhishi/296204.html 转载请保留说明!
下一篇链接:https://www.jiuchutong.com/zhishi/296205.html
免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络