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 ...
随机推荐
- 5.安装hbase
下载安装包并解压设置hbase环境变量配置hbase-site.xml启动hbase检测hbase启动情况测试hbase shell 下载安装包并解压 https://mirrors.tuna.tsi ...
- HDU 1042 N!(高精度乘)
Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in ...
- df -h 卡住
mount 检查是否有挂载nfs的分区 网络挂载 如果有请umount -l /相应目录 umount -l 10.74.82.205:/letv/fet/nfs ...
- wpa_supplicant上行接口浅析
摘自http://blog.csdn.net/fxfzz/article/details/6176414 wpa_supplicant提供的接口 从通信层次上划分, 上行接口:wpa_supplica ...
- Greedy Gift Givers 贪婪的送礼者
Description 对于一群要互送礼物的朋友,TRW要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人.然而,在任何一群 ...
- PHP 将一个字符串部分字符用$re替代隐藏
<?php/** * 将一个字符串部分字符用$re替代隐藏 * @param string $string 待处理的字符串 * @param int $start 规定在字符串的何处开始, * ...
- <Effective C++>读书摘要--Ctors、Dtors and Assignment Operators<一>
<Item 5> Know what functions C++ silently writes and calls 1.If you don't declare them yoursel ...
- 【Redis】- 延时任务
引言 在开发中,往往会遇到一些关于延时任务的需求.例如 生成订单30分钟未支付,则自动取消 生成订单60秒后,给用户发短信 对上述的任务,我们给一个专业的名字来形容,那就是延时任务.那么这里就会产生一 ...
- Windows API封装:LoadLibrary/FreeLibrary
LoadLibrary/LoadLibraryEx用来加载DLL到自己的进程空间,使用完用FreeLibrary释放,一般使用方式如下: HINSTANCE hInstRich = ::Load ...
- TCP协议详解7层和4层解析(美团,阿里) 尤其是三次握手,四次挥手 具体发送的报文和状态都要掌握
如果想了解HTTP的协议结构,原理,post,get的区别(阿里面试题目),请参考:HTTP协议 结构,get post 区别(阿里面试) 这里有个大白话的解说,可以参考:TCP/IP协议三次握手和四 ...