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}
        }
    }

      在无法使用history模式的情况下,使用另外一种方式:

    const Foo = {
        template: `
            <div>
                <div><a href="javascript:void(0)" @click="goAnchor('#anchor-'+index)" v-for="index in 20"> {{index}} </a></div>
                <div :id="'anchor-'+index" class="item" v-for="index in 20">{{index}}</div>
            </div>
        `,
        methods: {
            goAnchor(selector) {
                var anchor = this.$el.querySelector(selector)
                document.body.scrollTop = anchor.offsetTop
            }
        }
    }
    const Bar = {
        template: '<div>bar</div>'
    }
    
    const routes = [
      { path: '/foo', component: Foo },
      { path: '/bar', component: Bar }
    ]
    
    const router = new VueRouter({
      routes,
    })
    
    const app = new Vue({
      router
    }).$mount('#app')
  • 相关阅读:
    力扣----4. 有效的括号(JavaScript, Java实现)
    力扣----3. 无重复字符的最长子串(JavaScript, Java实现)
    力扣----2. 两数相加(JavaScript, Java实现)
    力扣----1. 两数之和(JavaScript, Java实现)
    sql server实现copy data功能的存储过程(公共代码)
    inner join 与 left join 与 right join之间的区别
    redux
    Spring Boot-3 (@PathVariable和@RequestParam)
    小程序 wx.request
    小程序 -- ui布局
  • 原文地址:https://www.cnblogs.com/goloving/p/9211233.html
Copyright © 2011-2022 走看看