当前位置:首页 > 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实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-ra…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生改…

vue怎么实现

vue怎么实现

Vue 实现方法 Vue 是一个流行的前端框架,用于构建用户界面。以下是几种常见的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。 <input v-m…

vue tab实现

vue tab实现

Vue Tab 实现方法 使用动态组件实现 Tab 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现 Ta…

vue实现 弹窗

vue实现 弹窗

Vue 实现弹窗的基本方法 使用 Vue 实现弹窗可以通过多种方式,包括组件化、指令、插件等。以下是几种常见的实现方法。 组件化实现弹窗 创建一个独立的弹窗组件,通过 props 控制显示与隐藏。…

vue 实现音乐

vue 实现音乐

Vue 实现音乐播放功能 在 Vue 中实现音乐播放功能,可以通过 HTML5 的 <audio> 元素或第三方库如 howler.js 来实现。以下是两种方法的详细步骤: 使用 HTM…