首页 新闻 会员 周边

(急)vue记录滚动位置,返回时返回到刚刚访问的位置

0
悬赏园豆:20 [待解决问题]

类似某宝的商品列表页,点进去之后,再返回还是刚刚看的位置。

savedPosition用过不知道怎么没有生效
问题补充:

js试过的方法:

mounted(){
let map = {};
window.onhashchange = function() {
document.body.scrollTop = 0;
}
window.onscroll = function() {
if (document.body.scrollTop) {
// 存
map[location.hash] = document.body.scrollTop;
} else {
var timer = null;
timer = setInterval(function(){
if (document.body.scrollTop == map[location.hash]) {
clearInterval(timer);
} else {
document.body.scrollTop = map[location.hash];
}
}, 20);
}
}
}

vue scrollBehavior试过的方法:

const scrollBehavior = (to, from, savedPosition) => {
// if (savedPosition) {
// // savedPosition is only available for popstate navigations.
// return savedPosition
// } else {
// const position = {}
// // new navigation.
// // scroll to anchor by returning the selector
// if (to.hash) {
// position.selector = to.hash
//
// // specify offset of the element
// if (to.hash === '#shopDetail') {
// position.offset = { y: 100 }
// }
// }
// // check if any matched route config has meta that requires scrolling to top
// if (to.matched.some(m => m.meta.scrollToTop)) {
// // cords will be used if no selector is provided,
// // or if the selector didn't match any element.
// position.x = 0
// position.y = 500
// }
// // if the returned position is falsy or an empty object,
// // will retain current scroll position.
// return position
// }
// }

蓝天海礁的主页 蓝天海礁 | 初学一级 | 园豆:182
提问于:2017-08-07 19:59
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册