当前位置:首页 > VUE

vue实现左右菜单联动实现

2026-03-28 05:32:14VUE

实现左右菜单联动的基本思路

左右菜单联动通常指左侧为分类菜单,右侧为内容区域,点击左侧菜单时右侧内容自动滚动到对应位置,反之滚动右侧时左侧菜单高亮对应项。核心在于通过监听滚动事件和点击事件实现双向绑定。

基于Vue3的代码实现

模板结构

<template>
  <div class="container">
    <div class="left-menu">
      <div 
        v-for="(item, index) in menuList" 
        :key="index"
        :class="{ 'active': activeIndex === index }"
        @click="handleMenuClick(index, $event)"
      >
        {{ item.title }}
      </div>
    </div>
    <div class="right-content" ref="contentRef" @scroll="handleContentScroll">
      <div 
        v-for="(item, index) in menuList" 
        :key="index"
        :ref="setContentRef"
        class="content-section"
      >
        <h2>{{ item.title }}</h2>
        <p>{{ item.content }}</p>
      </div>
    </div>
  </div>
</template>

脚本部分

<script setup>
import { ref, onMounted } from 'vue'

const menuList = [
  { title: '分类1', content: '内容1...' },
  { title: '分类2', content: '内容2...' },
  // 更多分类...
]

const activeIndex = ref(0)
const contentRef = ref(null)
const contentItems = ref([])

const setContentRef = (el) => {
  if (el) contentItems.value.push(el)
}

const handleMenuClick = (index, event) => {
  activeIndex.value = index
  const targetEl = contentItems.value[index]
  contentRef.value.scrollTo({
    top: targetEl.offsetTop,
    behavior: 'smooth'
  })
}

const handleContentScroll = () => {
  const scrollTop = contentRef.value.scrollTop
  const scrollHeight = contentRef.value.scrollHeight
  const clientHeight = contentRef.value.clientHeight

  contentItems.value.forEach((item, index) => {
    const itemTop = item.offsetTop
    const itemBottom = itemTop + item.offsetHeight

    if (scrollTop >= itemTop - 50 && scrollTop < itemBottom - 50) {
      activeIndex.value = index
    }
  })
}

onMounted(() => {
  // 初始化第一个菜单项高亮
  activeIndex.value = 0
})
</script>

样式部分

<style scoped>
.container {
  display: flex;
  height: 100vh;
}

.left-menu {
  width: 200px;
  overflow-y: auto;
  border-right: 1px solid #eee;
}

.left-menu div {
  padding: 12px 16px;
  cursor: pointer;
}

.left-menu div.active {
  background-color: #1890ff;
  color: white;
}

.right-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

.content-section {
  margin-bottom: 40px;
  min-height: 300px;
}
</style>

实现关键点说明

双向绑定机制
通过handleMenuClick处理左侧菜单点击,使用scrollTo方法平滑滚动到对应内容区域。通过handleContentScroll监听右侧滚动事件,计算当前可视区域对应的内容块并更新左侧菜单高亮状态。

vue实现左右菜单联动实现

性能优化
滚动事件使用防抖处理避免频繁触发,示例中未展示但实际项目建议添加:

import { debounce } from 'lodash-es'
const handleContentScroll = debounce(() => { ... }, 100)

动态内容处理
使用setContentRef收集所有内容块的DOM引用,确保能准确计算各内容块位置。当内容动态加载时需清空contentItems重新收集。

vue实现左右菜单联动实现

扩展功能建议

锚点导航
可在URL哈希中记录当前激活的菜单项,实现浏览器前进后退导航:

watch(activeIndex, (val) => {
  window.location.hash = `#${menuList[val].title}`
})

响应式设计
通过媒体查询在小屏幕下转换为上下布局:

@media (max-width: 768px) {
  .container { flex-direction: column; }
  .left-menu { width: 100%; height: 60px; }
}

标签: 菜单vue
分享给朋友:

相关文章

vue实现store

vue实现store

Vue 实现 Store 在 Vue 中,可以通过 Vuex 或 Pinia 实现全局状态管理(Store)。以下是两种主流方案的实现方法。 使用 Vuex 实现 Store Vuex 是 Vue…

antd vue 实现

antd vue 实现

使用 Ant Design Vue 实现功能 Ant Design Vue 是基于 Vue.js 的 UI 组件库,提供了丰富的组件和设计规范。以下是一些常见功能的实现方法。 安装 Ant Desi…

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

vue实现ajax

vue实现ajax

Vue 中实现 AJAX 的几种方法 在 Vue 中实现 AJAX 请求可以通过原生 JavaScript 的 XMLHttpRequest、第三方库如 axios 或 fetch API 来完成。以…

vue  实现tab

vue 实现tab

实现 Tab 切换功能 在 Vue 中实现 Tab 切换功能可以通过动态组件或条件渲染完成。以下是两种常见方法: 使用 v-if 或 v-show 实现条件渲染 通过绑定 currentTab 变量…

vue实现刷卡

vue实现刷卡

Vue 实现刷卡效果 在 Vue 中实现刷卡效果可以通过 CSS 动画和 Vue 的过渡系统结合完成。以下是一种常见的实现方式: 基础实现方法 安装 Vue 过渡依赖(如果尚未安装): npm i…