zoukankan      html  css  js  c++  java
  • Vue scrollBehavior 滚动行为

    使用前端路由,当切换到新路由时,想要页面滚到顶部,或者是保持原先的滚动位置,就像重新加载页面那样。 vue-router 能做到,而且更好,它让你可以自定义路由切换时页面如何滚动。

    注意: 这个功能只在 HTML5 history 模式下可用。

    当创建一个 Router 实例,你可以提供一个 scrollBehavior 方法:

    const router = new VueRouter({
      routes: [...],
      scrollBehavior (to, from, savedPosition) {
        // return 期望滚动到哪个的位置
      }
    })

    scrollBehavior 方法接收 to 和 from 路由对象。第三个参数 savedPosition 当且仅当 popstate 导航 (通过浏览器的 前进/后退 按钮触发) 时才可用。

    这个方法返回滚动位置的对象信息,长这样:

    • { x: number, y: number }
    • { selector: string, offset? : { x: number, y: number }} (offset 只在 2.6.0+ 支持)

    如果返回一个 falsy (译者注:falsy 不是 false参考这里)的值,或者是一个空对象,那么不会发生滚动。

    举例:

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

    对于所有路由导航,简单地让页面滚动到顶部。

    返回 savedPosition,在按下 后退/前进 按钮时,就会像浏览器的原生表现那样:

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

    如果你要模拟『滚动到锚点』的行为:

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

    我们还可以利用路由元信息更细颗粒度地控制滚动。查看完整例子:

    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
        }
        // 如果meta中有scrollTop
        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
      }
    }

    与keepAlive结合,如果keepAlive的话,保存停留的位置:

    scrollBehavior (to, from, savedPosition) {
         if (savedPosition) {
                return savedPosition
        } else {
            if (from.meta.keepAlive) {
              from.meta.savedPosition = document.body.scrollTop;
            }
            return { x: 0, y: to.meta.savedPosition ||0}
        }
    }
                   
  • 相关阅读:
    测试开发技术
    测试开发技术难题与解决
    .gitignore文件
    mysql 子查询 联结 组合查询
    DTL
    jquery 基础
    Django操作数据库
    git 进阶篇
    miniconda使用
    pycharm之django基本配置
  • 原文地址:https://www.cnblogs.com/sophie_wang/p/7880261.html
Copyright © 2011-2022 走看看