当前位置:首页 > VUE

vue实现跳转高亮

2026-03-28 20:57:21VUE

Vue 实现跳转高亮的方法

使用 router-linkactive-class

在 Vue Router 中,router-link 组件会自动为当前激活的路由链接添加一个 router-link-active 类。可以通过 active-class 属性自定义类名。

<router-link to="/home" active-class="active">Home</router-link>
<router-link to="/about" active-class="active">About</router-link>
.active {
  color: #42b983;
  font-weight: bold;
}

动态绑定 classstyle

通过判断当前路由路径,动态绑定 classstyle 实现高亮效果。

<template>
  <div>
    <a
      href="#"
      :class="{ active: $route.path === '/home' }"
      @click="$router.push('/home')"
    >Home</a>
    <a
      href="#"
      :class="{ active: $route.path === '/about' }"
      @click="$router.push('/about')"
    >About</a>
  </div>
</template>
.active {
  color: #42b983;
  font-weight: bold;
}

使用 watch 监听路由变化

在组件中监听路由变化,动态设置高亮状态。

<template>
  <div>
    <a
      href="#"
      :class="{ active: activeTab === 'home' }"
      @click="navigateTo('home')"
    >Home</a>
    <a
      href="#"
      :class="{ active: activeTab === 'about' }"
      @click="navigateTo('about')"
    >About</a>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeTab: 'home'
    }
  },
  watch: {
    '$route.path'(newPath) {
      this.activeTab = newPath === '/home' ? 'home' : 'about'
    }
  },
  methods: {
    navigateTo(tab) {
      this.activeTab = tab
      this.$router.push(`/${tab}`)
    }
  }
}
</script>

使用 Vuex 管理高亮状态

对于复杂应用,可以使用 Vuex 统一管理高亮状态。

// store.js
export default new Vuex.Store({
  state: {
    activeTab: 'home'
  },
  mutations: {
    setActiveTab(state, tab) {
      state.activeTab = tab
    }
  }
})
<template>
  <div>
    <a
      href="#"
      :class="{ active: $store.state.activeTab === 'home' }"
      @click="navigateTo('home')"
    >Home</a>
    <a
      href="#"
      :class="{ active: $store.state.activeTab === 'about' }"
      @click="navigateTo('about')"
    >About</a>
  </div>
</template>

<script>
export default {
  methods: {
    navigateTo(tab) {
      this.$store.commit('setActiveTab', tab)
      this.$router.push(`/${tab}`)
    }
  }
}
</script>

结合 scrollBehavior 实现滚动高亮

在路由配置中设置 scrollBehavior,结合 IntersectionObserver 实现滚动高亮。

vue实现跳转高亮

// router.js
const router = new VueRouter({
  routes: [...],
  scrollBehavior(to, from, savedPosition) {
    if (to.hash) {
      return {
        selector: to.hash,
        behavior: 'smooth'
      }
    }
  }
})
<template>
  <div>
    <a href="#section1" :class="{ active: activeSection === 'section1' }">Section 1</a>
    <a href="#section2" :class="{ active: activeSection === 'section2' }">Section 2</a>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeSection: 'section1'
    }
  },
  mounted() {
    const observer = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        if (entry.isIntersecting) {
          this.activeSection = entry.target.id
        }
      })
    }, { threshold: 0.5 })

    document.querySelectorAll('section').forEach(section => {
      observer.observe(section)
    })
  }
}
</script>

标签: 跳转vue
分享给朋友:

相关文章

vue实现图库

vue实现图库

Vue 实现图库的基本方法 使用 Vue 实现图库功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用 Vue 和第三方库(如 Vue Gallery) 安装 vue-gallery 或…

vue实现截图

vue实现截图

Vue 实现截图的方法 使用 html2canvas 库 安装 html2canvas 库: npm install html2canvas 在 Vue 组件中使用: import html2ca…

vue实现动画

vue实现动画

Vue 实现动画的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及直接操作 CSS 或 JavaScript 动画。 使用 Vue 内置过渡系统 Vue 的 <…

vue 实现脚本

vue 实现脚本

Vue 实现脚本的方法 Vue.js 提供了多种方式来实现脚本功能,包括组件内脚本、混入(Mixins)、插件(Plugins)以及自定义指令等。以下是常见的实现方式: 组件内脚本 在 Vue 单文…

vue 实现遮罩

vue 实现遮罩

Vue 实现遮罩层的方法 使用固定定位和透明背景 在Vue中实现遮罩层可以通过CSS固定定位结合透明背景色完成。创建一个全屏遮罩组件,利用position: fixed覆盖整个视窗。 <tem…

vue 实现评分

vue 实现评分

Vue 实现评分功能 在 Vue 中实现评分功能可以通过多种方式完成,包括使用第三方组件库或自定义实现。以下是两种常见的方法: 使用第三方组件库(如 Element UI) Element UI 提…