首页 > 其他 > 详细

Vue控制路由滚动行为

时间:2019-04-21 11:01:46      阅读:134      评论:0      收藏:0      [点我收藏+]

跳转路由时,要求跳转到指定路由的某个地方,可以使用scrollBehavior方法控制。

用法:

scrollBehavior(to,from,savedPosition){   }

scrollBehavior方法可以返回x,y 坐标点,也可以返回指定的选择器

例子:

import Vue from ‘vue‘
import Router from ‘vue-router‘
 import Home from ‘../../view/Home.vue‘
 import Test from ‘../../view/Test.vue‘
 import News from ‘../../view/News.vue‘
Vue.use(Router)

export default new Router({
  routes: [
    {
      name:"Home",
      component:Home,
      path:"/"
    },
    {
      name:"Test",
      component:Test,
      path:"/test"
    },
    {
      name:"News",
      component:News,
      path:"/news"
    },
    {
      
      path:"*",
      redirect:"/"
    }
  ],
  mode:"history",
  //跳转到指定路由的指定坐标
  scrollBehavior(to,from,savedBehavior){
     if(to.path==="/test"){
        return {
          x:0,
          y:100
        }
     };
     // 跳转到指定的选择器
     if(to.path==="/news"){
      return {
        selector:".here"
      }
     }
  }
})

 

Vue控制路由滚动行为

原文:https://www.cnblogs.com/luguankun/p/10744058.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!