类似某宝的商品列表页,点进去之后,再返回还是刚刚看的位置。
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
// }
// }