javascript - vue-router scrollBehavior无效的问题
ringa_lee
ringa_lee 2017-04-11 11:38:31
[JavaScript讨论组]

复制了官方示例中的scrollBehavior方法

const scrollBehavior = (to, from, savedPosition) => {
  console.log(savedPosition);
  if (savedPosition) {
    // savedPosition is only available for popstate navigations.
    return savedPosition;
  }
  const position = {};
  // new navigation.
  // scroll to anchor by returning the selector
  if (to.hash) {
    position.selector = to.hash;
  }
  // 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 = 0;
  }
  // if the returned position is falsy or an empty object,
  // will retain current scroll position.
  return position;
};

根据官方文档,后退时应该会获得savedPosition,并自动跳转回之前的位置,我这里console.log(savedPosition)有输出位置,但没有跳转过去

我反复试验,得到的结果是,
在没有加keep-alive的前提下,
打开A页面,向下滚动一定距离,进入B页面,此时若B页面高度较高,超出了浏览器高度,即有滚动条的情况下,后退回A,A是能保持滚动位置的,但若B页面没有滚动条,则后退回A,A也滚回顶部,此时console.log(savedPosition)是有位置的,

但这应该不是正确的实践吧?

ringa_lee
ringa_lee

ringa_lee

全部回复(3)
伊谢尔伦

找到原因了
https://github.com/vuejs/vue-...

大家讲道理

是因为transition的动画问题么?如何处理,只能去掉?

伊谢尔伦

Hooking into transitions involves too many intricacies and depends on custom transition implementations, so vue-router is not going to support that as a built-in. It's possible to implement your own transition component for that purpose though.

vueJs/issues 中作者的意思是vue-router不在支持这个特性了。需要自己在transition组件中去实现

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号