前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vue中页面跳转滚动条置顶(总结)

vue中页面跳转滚动条置顶(总结)

作者头像
kirin
发布2020-10-27 10:35:49
2.5K0
发布2020-10-27 10:35:49
举报
文章被收录于专栏:Kirin博客

1.vue单个页面跳转时:

在此页面上mounted方法中设置滚动条的方法

1 2 3 4

mounted() {            // 切换页面时滚动条自动滚动到顶部        window.scrollTo(0,0);      }

2.全部页面,使用路由的钩子函数中设置(在router中main.js)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

router.afterEach((to,from,next) => {   window.scrollTo(0,0);   // chrome     document.body.scrollTop = 0     // firefox     document.documentElement.scrollTop = 0     // safari     window.pageYOffset = 0 }); //或 router.beforeEach((to, from, next) => {         // chrome     document.body.scrollTop = 0     // firefox     document.documentElement.scrollTop = 0     // safari     window.pageYOffset = 0     next() })

3.全部页面,使用路由vue-router自带的滚动条行为解决(router中的index.js文件中)

1 2 3 4 5 6 7 8

//页面跳转显示在顶部   scrollBehavior (to, from, savedPosition) {     if (savedPosition) {       return savedPosition     } else {       return { x: 0, y: 0 }     }   }

4.layout布局时全部页面跳转滚动条置顶,使用vue-router路由方法是无效的,在layout布局的router-view层使用update设置

1 2 3 4

updated(){     let dom = document.getElementsByClassName('layout-content')[0];     dom.scrollTo(0,0); }

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/10/26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档