当前位置:首页 > 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前端实现打印功能

vue前端实现打印功能

使用Vue实现前端打印功能 在Vue项目中实现打印功能可以通过多种方式完成,以下是几种常见的方法: 使用window.print()方法 这是最简单的打印方式,直接调用浏览器的打印功能。 meth…

vue 方法实现

vue 方法实现

在 Vue 中,方法的实现通常通过 methods 选项完成。以下是 Vue 方法实现的核心要点和示例: 基本方法定义 在 Vue 组件中定义方法时,需将函数声明放在 methods 对象内。这些方…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template&g…

vue实现toggle

vue实现toggle

Vue 实现 Toggle 功能 在 Vue 中实现 toggle(切换)功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 和 v-on 通过 v-model 绑定数据,结合…

vue 实现表单

vue 实现表单

Vue 表单实现方法 Vue.js 提供了多种方式实现表单,包括双向数据绑定、表单验证和动态表单生成。以下是常见的实现方法: 基础表单绑定 使用 v-model 指令实现表单元素与数据的双向绑定:…

vue事件实现

vue事件实现

Vue 事件实现方法 在Vue中,事件处理是通过v-on指令或@简写来实现的。以下是几种常见的事件处理方式: 使用v-on指令绑定事件 <button v-on:click="handleC…