当前位置: 首页 > news >正文

Vue 3 + Elementui + TypeScript 实现左侧菜单定位右侧内容

Vue 3 + Elementui + TypeScript 实现左侧菜单定位右侧内容

下面是一个完整的 Vue 3 组合式 API + TypeScript 实现方案,包含平滑滚动和当前激活项高亮功能。

样式展示

在这里插入图片描述

完整组件实现

<template><div class="container"><el-container><!-- 左侧菜单 --><el-aside class="left-menu"><button v-for="(section, index) in sections" :key="section.id" @click="scrollTo(index)" :class="{ active: activeSection === index }">{{ section.title }}</button></el-aside><!-- 右侧内容 --><el-main class="right-content"><section v-for="(section, index) in sections" :key="section.id" :ref="(el) => setSectionRef(el, index)"><h2 :id="section.id">{{ section.title }}</h2><p>{{ section.content }}</p></section></el-main></el-container></div>
</template><script lang="ts">
import { defineComponent, ref, onMounted, onBeforeUnmount } from 'vue';interface Section {id: string;title: string;content: string;
}export default defineComponent({name: 'MenuContentNavigation',setup() {// 定义内容区域数据const sections = ref<Section[]>([{id: 'section-1',title: '第一部分',content: '这是第一部分的内容...',},{id: 'section-2',title: '第二部分',content: '这是第二部分的内容...',},{id: 'section-3',title: '第三部分',content: '这是第三部分的内容...',},]);const activeSection = ref<number>(0);const sectionRefs = ref<HTMLElement[]>([]);let observer: IntersectionObserver | null = null;// 设置内容区域引用const setSectionRef = (el: any, index: number) => {if (el) {sectionRefs.value[index] = el;}};// 滚动到指定区域const scrollTo = (index: number) => {if (sectionRefs.value[index]) {sectionRefs.value[index].scrollIntoView({behavior: 'smooth',block: 'start',});activeSection.value = index;}};// 初始化 Intersection Observerconst initObserver = () => {const options = {root: document.querySelector('.right-content'),rootMargin: '0px',threshold: 0.5,};observer = new IntersectionObserver((entries) => {entries.forEach((entry) => {if (entry.isIntersecting) {const index = sectionRefs.value.findIndex((ref) => ref === entry.target);if (index !== -1) {activeSection.value = index;}}});}, options);sectionRefs.value.forEach((section) => {if (section) {observer?.observe(section);}});};onMounted(() => {initObserver();});onBeforeUnmount(() => {if (observer) {observer.disconnect();}});return {sections,activeSection,setSectionRef,scrollTo,};},
});
</script><style scoped>
.container {display: flex;height: 100vh;
}.left-menu {width: 200px;padding: 20px;background-color: #f5f5f5;display: flex;flex-direction: column;gap: 10px;position: sticky;top: 0;height: 100vh;overflow-y: auto;
}.left-menu button {padding: 10px 15px;text-align: left;border: none;background: none;cursor: pointer;border-radius: 4px;transition: all 0.3s;
}.left-menu button:hover {background-color: #e0e0e0;
}.left-menu button.active {background-color: #2196f3;color: white;font-weight: bold;
}.right-content {flex: 1;padding: 20px;overflow-y: auto;
}.right-content section {min-height: 100vh;padding: 20px 0;border-bottom: 1px solid #eee;
}.right-content h2 {margin-top: 0;padding-top: 60px; /* 为固定导航栏留出空间 */margin-top: -40px; /* 抵消部分padding-top */
}
</style>

关键功能说明

  1. TypeScript 类型定义

    • 定义了 Section 接口来描述每个内容区域的类型
    • 使用泛型 ref<Section[]>ref<number> 确保类型安全
  2. 响应式引用管理

    • 使用 :ref="(el) => setSectionRef(el, index)" 动态设置引用
    • 将引用存储在 sectionRefs 数组中以便访问
  3. 平滑滚动

    • scrollTo 方法使用 scrollIntoView 实现平滑滚动
    • 包含 behavior: 'smooth'block: 'start' 选项
  4. Intersection Observer

    • 自动检测当前可见区域
    • 当内容区域进入视口时更新激活状态
    • 在组件卸载时正确清理观察器
  5. 样式处理

    • 使用 sticky 定位左侧菜单
    • 为固定导航栏添加了 padding 和 margin 补偿
    • 添加了平滑的过渡效果

使用注意事项

  1. 如果需要支持旧版浏览器,应添加 Intersection Observer 的 polyfill
  2. 可以根据实际需求调整观察器的 threshold
  3. 如果内容区域高度很大,可以考虑使用虚拟滚动优化性能
  4. 在移动设备上可能需要调整布局为垂直排列
http://www.xdnf.cn/news/1285849.html

相关文章:

  • 石英加速度计如何实现高精度测量?
  • 深度贴:前端网络基础及进阶(3)
  • 鲲鹏arm服务器安装neo4j社区版,实现图书库自然语言检索基础
  • 地图可视化实践录:显示地理区域图
  • 自然语言处理关键库解析和使用方法- FuzzyWuzzy
  • 虚拟机一站式部署Claude Code 可视化UI界面
  • 豆包 + 蘑兔 AI:你的创作搭子
  • 运维学习Day22——Anisible自动化与基本使用
  • Kafka的一条消息的写入和读取过程原理介绍
  • kafka 消费者组的概念是什么?它是如何实现消息的点对点和发布/订阅模式?
  • PO、BO、VO、DTO、POJO、DAO、DO基本概念
  • 开源!!! htop移植到OpenHarmony
  • 【网络运维】Linux和自动化: Ansible基础实践
  • ncurses 6.5 交叉编译移植到OpenHarmomy
  • 【软考中级网络工程师】知识点之 IP QoS 技术
  • 小红书笔记信息获取_实在智能RPA源码解读
  • 【Redis优化深度剖析:如何通过读写分离提升系统性能】
  • 【限时分享:Hadoop+Spark+Vue技术栈电信客服数据分析系统完整实现方案
  • Rocky Linux 10 部署 Kafka 集群
  • Bevy渲染引擎核心技术深度解析:架构、体积雾与Meshlet渲染
  • AI-调查研究-49-大数据调研报告 发展历程:从概念诞生到多元化生态1997-2025
  • msyql中,max_connections和max_user_connections区别
  • 【DL】深层神经网络
  • 记录docker使用kong consul postgresql配置dns异常解决
  • SQL180 每类试卷得分前3名
  • 【Redis在在线表单提交防重复机制中的应用策略】
  • 移动端调用大模型详解
  • Web学习笔记5
  • [git] 重配ssh key | 解决冲突
  • 一键生成 Android 适配不同分辨率尺寸的图片