看源码的时候看到这个属性:

新手自然不知道这个是什么东西了,查了下vue  API:

https://router.vuejs.org/en/advanced/scroll-behavior.html

上面这个的意思就是当转到一个新的页面时,定位到最顶端。

Scroll Behavior

When using client-side routing, we may want to scroll to top when navigating to a new route, or preserve the scrolling position of history entries just like real page reload does. vue-router allows you to achieve these and even better, allows you to completely customize the scroll behavior on route navigation.

Note: this feature only works in HTML5 history mode.

When creating the router instance, you can provide the scrollBehavior function:

const router = new VueRouter({
routes: [...],
scrollBehavior (to, from, savedPosition) {
// return desired position
}
})

The scrollBehavior function receives the to and from route objects. The third argument, savedPosition, is only available if this is a popstatenavigation (triggered by the browser's back/forward buttons).

The function can return a scroll position object. The object could be in the form of:

  • { x: number, y: number }
  • { selector: string }

If a falsy value or an empty object is returned, no scrolling will happen.

For example:

scrollBehavior (to, from, savedPosition) {
return { x: 0, y: 0 }
}

This will simply make the page scroll to top for all route navigations.

Returning the savedPosition will result in a native-like behavior when navigating with back/forward buttons:

scrollBehavior (to, from, savedPosition) {
if (savedPosition) {
return savedPosition
} else {
return { x: 0, y: 0 }
}
}

If you want to simulate the "scroll to anchor" behavior:

scrollBehavior (to, from, savedPosition) {
if (to.hash) {
return {
selector: to.hash
}
}
}

We can also use route meta fields to implement fine-grained scroll behavior control. Check out a full example here.

vue2路由之指定滑动位置scrollBehavior-(载转)的更多相关文章

  1. vue2路由之指定滑动位置scrollBehavior

    看源码的时候看到这个属性: 新手自然不知道这个是什么东西了,查了下vue  API: https://router.vuejs.org/en/advanced/scroll-behavior.html ...

  2. json解析,异步下载(listview仅滑动时加载)Demo总结

    异步加载的练习demo 主要涉及知识点: 1.解析json格式数据,主要包括图片,文本 2.使用AsynTask异步方式从网络下载图片 3.BaseAdapter的"优雅"使用 4 ...

  3. JS控制div跳转到指定的位置的解决方案总结

    总结一下自己在写这个需求遇到的问题,相信大家应该是经常遇到的.即要求滚轮滚动到指定的位置.先看下基本的解决方案. 1.给链接a加个#的方式来实现跳转.(锚点方法)这里直接贴下代码: html页面: & ...

  4. express框架路由配置及congtroller自动加载

    express框架在node官方推荐的一个框架,关于如何入门的文章,已经很多了,我就不在累赘了,本文的核心是如何修改文件使得更接近一个MVC的框架 express原生是通过require的方式实现了模 ...

  5. springboot打包去除资源文件,启动时指定配置文件位置,使用log4j2替换默认logback

    springboot打包时,去掉资源文件 <build> <resources> <resource> <directory>src/main/reso ...

  6. JS控制div跳转到指定的位置的几种解决方案总结

    原文:http://www.jb51.net/article/96574.htm 这篇文章主要介绍了JS控制div跳转到指定的位置的几种解决方案总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考 ...

  7. Android RecyclerView使用 及 滑动时加载图片优化方案

    1.控制线程数量 + 数据分页加载2.重写onScrollStateChanged方法 这个我们后面再谈,下面先来看看RecyclerView控件的使用及我们为什么选择使用它 RecyclerView ...

  8. [Android Pro] 精确记录和恢复ListView滑动位置

    reference to : http://blog.csdn.net/welovesunflower/article/details/7926512 工作中遇到一个需求,对ListView某一项操作 ...

  9. VS2013编译经常卡在正在从以下位置加载xxx.dll的符号

    换了系统后,重新下载了一个vs2013 with update2安装,编译的时候总是卡在 正在从以下位置加载xxx.dll的符号 如图: 解决方法: 进入VS---工具---选项----调试----符 ...

随机推荐

  1. 【C/C++】面相对象开发之封装

    封装继承多态是面向对象程序开发的基础概念.是实现面向对象的基本要素. 封装 程序开发,最核心价值,是数据. 程序其实是读取数据,操作数据,保存数据等一系列操作. 那么经过良好组织过的数据,将使编程事半 ...

  2. 一文简述JAVA内部类和异常

    内部类和异常 内部类 在一个类的内部定义的一个类,例如,A类中定义了一个B类,则B类相对于A类就是内部类,而A类相对于B类就是外部类 成员内部类 静态内部类 局部内部类 匿名内部类 成员内部类 pub ...

  3. 小程序使用 Promise.all 完成文件异步上传

    小程序使用 Promise.all 完成文件异步上传 extends [微信小程序开发技巧总结(二) -- 文件的选取.移动.上传和下载 - Kindear - 博客园 (cnblogs.com)] ...

  4. python读取excel数据为json格式(兼容xls\xlsx)

    做自动化时需要从excel读取数据: 本文实现将excel文件数据读取为json格式,方便自动化调用 读取xls文件 使用xlrd读取xls文件代码: import xlrd def read_xls ...

  5. Jenkins 自动触发执行的配置

    1. 两种触发方式 2. jenkins 和 github 同步配置 ngrok 安装 webhook 配置 1. 两种触发条件 Jenkins 中建立的任务是可以设置自动触发,更进一步的实现自动化. ...

  6. 一键打造vim ide 支持python golang shell等高级特性

    1.vim-for-devops github: https://github.com/yxxhero/vim_for_devops 利用vim插件打造支持python.shell.golang的id ...

  7. IDEA关于-javaagent中文路径导致无法debug的初步解决方案

    IDEA明明是装在D盘的纯英文路径,而且java的相关环境变量全部是在英文路径,结果还是因为中文路径的问题导致无法debug.仔细观察,发现这个中文路径是作为java的-javaagent的参数输入的 ...

  8. 千位分隔符,音频音量加强,transform 垂直居中

    1.最近做阴阳师日本官网,其中有个功能是获取预约人数,设计稿上的人数是这样151,567,000,想了想自己写还有点麻烦,于是网上copy了代码,再修改了下. 其中,有一点需要注意的是:函数中的str ...

  9. UVA11300分金币

    题意:      圆桌旁作者n个人,每个人都有一定数量的金币,他们每次可以给相邻的人一枚金币(可以给多次),问所有人金币数都相同的话最少要给多少次金币.思路:       这个题目感觉很好,首先我们可 ...

  10. hdu4888 最大流(构造矩阵)

    题意:       让你构造一个矩阵,满足每一行的和,和每一列的和都等于他给的,还要判断答案是否唯一,还有一点就是矩阵内所有的数字都是[0,k]范围的. 思路:       这个题目看完就让我想起了h ...