vue2路由之指定滑动位置scrollBehavior
看源码的时候看到这个属性:
新手自然不知道这个是什么东西了,查了下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 popstate
navigation (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的更多相关文章
- vue2路由之指定滑动位置scrollBehavior-(载转)
看源码的时候看到这个属性: 新手自然不知道这个是什么东西了,查了下vue API: https://router.vuejs.org/en/advanced/scroll-behavior.html ...
- [Android Pro] 精确记录和恢复ListView滑动位置
reference to : http://blog.csdn.net/welovesunflower/article/details/7926512 工作中遇到一个需求,对ListView某一项操作 ...
- JS控制div跳转到指定的位置的解决方案总结
总结一下自己在写这个需求遇到的问题,相信大家应该是经常遇到的.即要求滚轮滚动到指定的位置.先看下基本的解决方案. 1.给链接a加个#的方式来实现跳转.(锚点方法)这里直接贴下代码: html页面: & ...
- MYSQL 为表指定文件位置 data directory
背景知识: 如果表不指定文件位置,它会保存到 data/database_name/table_file;其中data在你指定的安装目录下,为了提高IO我们尽可能的 用到多个硬盘的IO能力,这个就需要 ...
- 如何在Android Studio中指定NDK位置?
如何在Android Studio中指定NDK位置? 问题描述 NDK已经手工下载解包在本地: D:\Portable\android-ndk-r13b 每次创建支持C++项目时,都提示NDK没配置, ...
- Selenium 指定浏览器位置
在脚本开头要指定浏览器位置. public static void main(String[] args) throws InterruptedException, IOException { Sys ...
- springboot打包去除资源文件,启动时指定配置文件位置,使用log4j2替换默认logback
springboot打包时,去掉资源文件 <build> <resources> <resource> <directory>src/main/reso ...
- Android ListView滚动到指定的位置
这篇文章主要给大家介绍了Android中的ListView如何滚动到指定的位置,文章给出了两种解决的方法,并给出详细的示例代码,相信会对大家的理解和学习很有帮助,有需要的朋友们下面来一起看看吧. 本文 ...
- 在textarea中鼠标指定的位置插入字符或表情
有些时候我们已经在textarea中输入了一些字符,然后想在鼠标指定的位置插入表情或者字符,这就需要用到jquery的一个小插件了. 代码如下: (function ($) { $.fn.extend ...
随机推荐
- solidity中的memory和 storage详解
Solidity是一种智能合约高级语言,运行在Ethereum虚拟机(EVM)之上.这里我会讲解一下关键字storage和memory的区别. storage的结构是在合约部署创建时,根据你的合约中状 ...
- SpringCloud IDEA 教学 (四) 断路器(Hystrix)
写在开始 在SpringCloud项目中,服务之间相互调用(RPC Remote Procedure Call —远程过程调用),处于调用链路底层的服务产生不可用情况时,请求会产生堆积使得服务器线程阻 ...
- Python中函数的参数-arguments
归纳起来,Python中函数的定义形式和调用形式主要有如下几种形式: # 函数的定义形式 def func(name) # 匹配positional参数或者keyword参数 def func(nam ...
- Thunder团队第五周 - Scrum会议6
Scrum会议6 小组名称:Thunder 项目名称:i阅app Scrum Master:邹双黛 工作照片: 宋雨同学在拍照,所以不在照片内. 参会成员: 王航:http://www.cnblogs ...
- NSTimer使用注意事项
1.scheduled开头和非schedule的开头方法的区别.系统框架提供了几种创建NSTimer的方法,其中以scheduled开头的方法会自动把timer加入当前run loop,到了设定的时间 ...
- DataSet和List 泛型之间互相转换 (转载)
//DataSet与泛型集合间的互相转换 //利用反射机制将DataTable的字段与自定义类型的公开属性互相赋值. //注意:从DataSet到IList<T>的转换,自定义类型的公开属 ...
- 如何彻底解决adb 5037端口被占用
在进行安卓开发的时候是不是经常碰到adb端口被占用的情况? 解决这个问题的方法很简单,只需要设置一个系统环境变量就可以搞定. 设置方法: 增加系统环境变量变量名称:ADNROID_ADB_SERVER ...
- SQL SERVER技术内幕之10 事务并发
1.事务 1.1事务的定义 事务是作为单个工作单元而执行的一系列操作.定义事务边界有显式和隐式两种.显式事务的定义以BEGIN TRAN作为开始,以COMMIT TRAN提交事务,以ROLLBACK ...
- MYsql 数据库密码忘记(Window)-2(mysql 5.7)
很久没用Mysql了,再次打开,发现用不了了,密码忘了,服务也无法打开,在cmd中输入mysql之后,显示不是内部指令. 看来问题是mysql服务打不开了 (1)在cmd中 输入net start m ...
- 静态方法不能使用this的原因 当没有实例对象时候 在静态方法里面传入this时会出现空指针异常现象 所以为了防止该现象 静态方法里面不能使用this
静态方法不能使用this的原因 当没有实例对象时候 在静态方法里面传入this时会出现空指针异常现象 所以为了防止该现象 静态方法里面不能使用this