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

东莞市官网网站建设抚顺网站seo

东莞市官网网站建设,抚顺网站seo,免费的公司网站怎么做,阿坝网站建设Vue 3 Elementui TypeScript 实现左侧菜单定位右侧内容 下面是一个完整的 Vue 3 组合式 API TypeScript 实现方案&#xff0c;包含平滑滚动和当前激活项高亮功能。 样式展示完整组件实现 <template><div class"container"><el-container><!-…

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.dtcms.com/a/504512.html

相关文章:

  • Python数据挖掘之集成技术
  • 网站建设广东猎头公司收费
  • 【Android】BottomNavigationView实现底部导航栏
  • 小程序开发需要多少钱宁波seo外包服务商
  • XSS攻击防范:从入门到精通
  • 做云购网站网站框架怎么做
  • 建中英文网站网络优化公司有哪些
  • 详细讲解java中的方法(适合新手小白)
  • vip解析网站怎么做网络搜索词排名
  • 最新网站域名如何寻找网站建设需求客户
  • 河北网站建设推广如何制作活动宣传网页
  • Linux memfree 的计算逻辑
  • 宁波网站建设软件开发医药公司网站建设备案
  • 一 网站建设的目的和目标辽宁网站建站优化公司
  • 网站开发本地环境seo排名怎么做
  • 网站首页置顶是怎么做单页展示网站
  • 微信网页上的网站怎么做的网站响应时间方案
  • ScheduledExecutorService
  • 网站首页新世纪建设集团有限公司长沙网站seo收费
  • 网站优化的论文pc网站建设企业
  • 各个视频网站怎么做原创作者南宁市建筑规划设计集团有限公司
  • 探索Word2Vec:从文本向量化到中文语料处理
  • 性能测试 | 认识性能测试的概念以及应用
  • 小企业网站建设论文东莞网站建设设计
  • 第23讲:文件操作
  • 网站如何不需要备案免费发广告的软件有哪些
  • 网站制作 需要什么网络技术唯品会网站建设特色
  • 如何提高网站点击率dns网站建设
  • 响应式布局的原理网站meta 优化建议
  • 网站建设设计收费网络销售面试问题有哪些