vue中router滚动行为scrollBehavior

效果:当切换tab时自动定位到指定高度

views/cityDetail.vue:

html 复制代码
<template>
  <div class="city-detail">{{$route.params.id}}</div>
</template>
<style>
.city-detail{
  height: 1500px;
  line-height: 500px;
  background-color: greenyellow;
  font-size: 100px;
  text-align: center;
}
</style>

App.vue:

html 复制代码
<template>
  <div id="app">
    <div class="sticky-header">头部导航栏(粘性布局)</div>
    <div class="box">vue router中的滚动行为</div>
    <div class="hot-cities">
      <h1>热门城市</h1>
      <div class="nav" id='nav'>
        <!-- <router-link tag='div' class="nav-link" active-class="active" :to="{path:'/cityDetail/1',hash:'#nav'}">城市1</router-link>
        <router-link tag='div' class="nav-link" active-class="active" :to="{path:'/cityDetail/2',hash:'#nav'}">城市2</router-link>
        <router-link tag='div' class="nav-link" active-class="active" :to="{path:'/cityDetail/3',hash:'#nav'}">城市3</router-link> -->
        <div v-for="(item,index) in cityList" :key="index" :class="['nav-link',{active:currentIndex===index}]" @click="handlePush(index)">{{item}}</div>
      </div>
    </div>
    <router-view></router-view>
  </div>
</template>
<script>
export default {
  data() {
    return { cityList: ['城市1', '城市2', '城市3'], currentIndex: 0 }
  },
  methods: {
    handlePush(index) {
      this.currentIndex = index
      this.$router.push({ path: '/cityDetail/' + (index + 1), hash: '#nav' })
    }
  }
}
</script>
<style lang="less" scoped>
.sticky-header {
  position: sticky;
  top: 0;
  width: 100%;
  line-height: 44px;
  background-color: blue;
  color: #fff;
  text-align: center;
  z-index: 1;
}
.box {
  width: 100%;
  line-height: 700px;
  background-color: orange;
  color: #fff;
  font-size: 100px;
  text-align: center;
}
.hot-cities {
  text-align: center;
  > h1 {
    padding: 20px 0;
  }
  .nav {
    display: flex;
    border-top: 1px solid #000;
    .nav-link {
      display: flex;
      height: 50px;
      flex: 1 1 33.33%;
      justify-content: center;
      align-items: center;
    }
    .active {
      color: greenyellow;
      background-color: #000;
    }
  }
}
</style>

router/index.js:

js 复制代码
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import CityDetail from '@/views/CityDetail'
const routes = [
  {
    path: '/cityDetail/:id',
    name: 'cityDetail',
    component: CityDetail
  }
]

const router = new VueRouter({
  mode: 'history',
  routes,
  scrollBehavior(to, from, savedPosition) {
    console.log(to, from, savedPosition)
    if (savedPosition) {
      return savedPosition // 浏览器上的前进后退按钮记录的位置对象
    } else {
      const position = {}
      position.selector = to.hash
      if (to.hash === '#nav') position.offset = { y: 150 }
      return position
    }
  }
})

export default router
相关推荐
Wyc7240925 分钟前
HTML:入门
前端·html
Sunny_lxm26 分钟前
自定义列甘特图,原生开发dhtmlxgantt根特图,根据数据生成只读根特图,页面展示html demo
前端·html·甘特图·dhtmlxgantt
熊猫钓鱼>_>1 小时前
建筑IT数字化突围:建筑设计企业的生存法则重塑
前端·javascript·easyui
GISer_Jing3 小时前
前端性能指标及优化策略——从加载、渲染和交互阶段分别解读详解并以Webpack+Vue项目为例进行解读
前端·javascript·vue
不知几秋3 小时前
数字取证-内存取证(volatility)
java·linux·前端
水银嘻嘻5 小时前
08 web 自动化之 PO 设计模式详解
前端·自动化
Zero1017137 小时前
【详解pnpm、npm、yarn区别】
前端·react.js·前端框架
&白帝&7 小时前
vue右键显示菜单
前端·javascript·vue.js
Wannaer7 小时前
从 Vue3 回望 Vue2:事件总线的前世今生
前端·javascript·vue.js
羽球知道7 小时前
在Spark搭建YARN
前端·javascript·ajax