vue 记录滚动位置_vue 路由跳转记住滚动位置,返回时回到上次滚动位置
方法一: 利用Keep-Alive和监听器
1.首先在路由中引入需要的模块
{
path: ‘/scrollDemo’,
name: ‘scrollDemo’,
meta: {
keepAlive:true //需要缓存
},
component: resolve=> { require([‘../view/scrollDemo.vue’],
resolve) }
}
2.在App.vue中设置缓存组件
//缓存组件跳转的页面
//非缓存组件跳转页面
3.在页面注册对应的事件
(1). 在data中定义一个初始值 scroll
(2). 在mouted中 ,mouted中的方法代表dom已经加载完毕
window.addEventListener('scroll', this.handleScroll);
(3).methods 用于存放页面函数
handleScroll () {this.scroll = document.documentElement &&document.documentElement.scrollTop
console.log(this.scroll)
}
4.activated 为keep-alive加载时调用
activated() {if(this.scroll > 0){
window.scrollTo(0, this.scroll);this.scroll = 0;
window.addEventListener('scroll', this.handleScroll);
}
}
5.deactivated 页面退出时关闭事件 防止其他页面出现问题
deactivated(){
window.removeEventListener('scroll', this.handleScroll);
}
方法二:利用beforeRouteLeave和watch
main.js中:
var store = newVuex.Store({ //记得先引入vuex
state: {
recruitScrollY:0},
getters: {
recruitScrollY: state=>state.recruitScrollY
},
mutations: {
changeRecruitScrollY(state, recruitScrollY) {
state.recruitScrollY=recruitScrollY;
}
},
actions: {
},
modules: {}
})
组件中(/flashSaleListX为当前组件,即需要记住滚动条位置的组件):
methods:{
isTabRoute:function() {if (this.$route.path === '/flashSaleListX') {
let recruitScrollY= this.$store.state.recruitScrollY
document.documentElement.scrollTop=recruitScrollY;
}
}
},
watch: {'$route': 'isTabRoute',
},
beforeRouteLeave(to, from, next) {
let position= document.documentElement && document.documentElement.scrollTop; //记录离开页面时的位置
if (position == null) position = 0
this.$store.commit('changeRecruitScrollY', position) //离开路由时把位置存起来
next()
}
方法三:(适用于方法二获取不到滚动位置)
组件中:
··· 内容···
beforeRouteEnter(to, from, next) {
next(vm=>{
const div1=vm.$refs.div1//记录滚动高度
div1.scrollTop =vm.scroll
})
},
beforeRouteLeave(to, from, next) {
const div1= this.$refs.div1;this.scroll =div1.scrollTop; //data中记得定义变量scroll
next()
}
注:在路由配置中,记住滚动的页面keep-alive需为true
vue 记录滚动位置_vue 路由跳转记住滚动位置,返回时回到上次滚动位置相关推荐
- Vue中this.$router.push路由跳转,刷新参数消失
Vue中this.$router.push路由跳转,刷新参数消失 this.$router.push({name:"",params:{id:""}}) nam ...
- css 平移到某个位置_怎么实现点击div移动到某个位置,再点击时回到原来的位置...
问题:怎么实现点击div移动到某个位置,再点击时回到原来的位置?哪位好心人可以提示一下怎么更优雅地实现这个效果,感觉我写的方法有点乱.非常感谢! 效果图: 圈圈运动 //css代码 * { paddi ...
- vue 函数 路由跳转_vue路由跳转的方式
vue路由跳转有四种方式 1. router-link 2. this.$router.push() (函数里面调用) 3. this.$router.replace() (用法同push) 4. t ...
- vue路由跳转子组件_Vue路由跳转
现有如下场景,点击父组件的li元素跳转到子组件中,并携带参数,便于子组件获取数据. 父组件中: methods: 方案一: getDescribe(id) { // 直接调用$router.push ...
- vue路由跳转动画_Vue路由跳转动画
1.App.vue中添加 export default { name: 'App', data(){ return{ animate: "" } }, watch: { $rout ...
- vue地址栏输入路由跳转到首页_Vue路由跳转到新页面时 默认在页面最底部 而不是最顶部 的解决...
php截取字符串函数 public function sub_string($str, $len, $charset="utf-8") { if( !is_numeric($len ...
- vue学习:7、路由跳转
2019独角兽企业重金招聘Python工程师标准>>> <body><div id="app"></div></body ...
- vue 判断权限过期_vue 路由权限
附上girhub 地址: loever/vue-FlexPaperViewergithub.com qq:3179689033 相互学习 我把router.js 分成router.js 和route ...
- id vue2路由传参_vue 路由跳转传参
正常跳转 this.$router.push('/index'); 1带参 toOrderDetail() { let orderUuid = this.order.uuid; this.$route ...
最新文章
- 计算机协会成立活动简报,“中国计算机学会CCF走进高校”活动在我校举行
- ICLR 2022论文列表公布,接收率高达32%
- 解决Eclipse中SVN图标不显示
- 在Coding.net创建项目开发
- 关于页面之间传参时有空格,中文及点击页面后退按钮的问题
- Windows新终端中玩转ASCII和Emoji游戏的正确姿势
- IOC操作Bean管理XML方式(注入外部bean)
- Java工作笔记-webService发布时通用的4个注解
- 【BZOJ 1449】 1449: [JSOI2009]球队收益 (最小费用流)
- 《spring-boot学习》-05-spring boot中redis应用
- 我不是九爷 带你了解 ansible
- 烟台移动dns网关_各地移动dns列表
- windows连接局域网打印机出现0x0000007c错误处理方法
- 十年前的生活小技巧文章
- 授课型英硕申请Ph.D (带奖)历程
- 计算机里不显示u盘盘符,U盘不显示盘符怎么办 U盘没了盘符解决方法【详解】...
- azw3文件怎么打开?
- ps4欧洲服务器注册,ps4pro注册港服教程
- 前端在线微信扫码支付的小功能
- s盒密码c语言源代码csdn,AES中S盒的c语言实现及代码