Vue.js组件开发-实现底部浮动导航页面滚动预览

news/2025/2/8 23:49:14/

使用 Vue 实现底部浮动导航,并且在页面滚动时进行预览

步骤概述

  1. 创建 Vue 项目:使用 Vue CLI 创建一个新的 Vue 项目。
  2. 设计页面结构:创建包含内容区域和底部浮动导航栏的页面结构。
  3. 实现滚动监听:监听页面滚动事件,根据滚动位置更新导航栏的激活状态。
  4. 实现导航跳转:点击导航栏的项时,页面滚动到相应的内容区域。

详细代码

1. 创建 Vue 项目

安装 Vue CLI,可以使用以下命令进行安装:

npm install -g @vue/cli

然后创建一个新的 Vue 项目:

vue create baike-navigation
cd baike-navigation
2. 编写组件代码

src/components 目录下创建一个名为 BaikeNavigation.vue 的组件,代码如下:

<template><div><!-- 内容区域 --><div class="content"><!-- 循环渲染每个章节 --><div v-for="section in sections" :key="section.id" :id="section.id"><h2>{{ section.title }}</h2><p>{{ section.content }}</p></div></div><!-- 底部浮动导航栏 --><div class="floating-nav"><!-- 循环渲染导航项 --><divv-for="section in sections":key="section.id":class="{ active: activeSection === section.id }"@click="scrollToSection(section.id)">{{ section.title }}</div></div></div>
</template><script>
export default {data() {return {// 定义章节数据sections: [{ id: 'section1', title: '章节 1', content: '这是章节 1 的内容。' },{ id: 'section2', title: '章节 2', content: '这是章节 2 的内容。' },{ id: 'section3', title: '章节 3', content: '这是章节 3 的内容。' },{ id: 'section4', title: '章节 4', content: '这是章节 4 的内容。' }],// 当前激活的章节 IDactiveSection: null};},mounted() {// 页面加载完成后,监听滚动事件window.addEventListener('scroll', this.handleScroll);// 初始化激活章节this.handleScroll();},beforeDestroy() {// 组件销毁前,移除滚动事件监听window.removeEventListener('scroll', this.handleScroll);},methods: {// 处理滚动事件的方法handleScroll() {const scrollTop = window.pageYOffset || document.documentElement.scrollTop;// 遍历每个章节for (let i = this.sections.length - 1; i >= 0; i--) {const section = this.sections[i];const element = document.getElementById(section.id);if (element && scrollTop >= element.offsetTop - 50) {// 如果滚动位置超过章节顶部,更新激活章节this.activeSection = section.id;break;}}},// 滚动到指定章节的方法scrollToSection(id) {const element = document.getElementById(id);if (element) {// 使用平滑滚动到指定章节element.scrollIntoView({ behavior: 'smooth' });}}}
};
</script><style scoped>
.content {padding: 20px;
}.floating-nav {position: fixed;bottom: 0;left: 0;width: 100%;background-color: #f5f5f5;border-top: 1px solid #ddd;display: flex;justify-content: center;align-items: center;padding: 10px 0;
}.floating-nav div {margin: 0 10px;cursor: pointer;
}.floating-nav div.active {color: #ff0000;
}
</style>
3. 在 App.vue 中使用组件
<template><div id="app"><BaikeNavigation /></div>
</template><script>
import BaikeNavigation from './components/BaikeNavigation.vue';export default {components: {BaikeNavigation}
};
</script><style>
#app {font-family: Avenir, Helvetica, Arial, sans-serif;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;text-align: center;color: #2c3e50;margin-top: 60px;
}
</style>

代码注释说明

  • template 部分

    • content 类的 div 用于显示内容区域,使用 v-for 指令循环渲染每个章节。
    • floating-nav 类的 div 是底部浮动导航栏,同样使用 v-for 指令循环渲染导航项。
    • :class="{ active: activeSection === section.id }" 根据当前激活的章节添加 active 类。
    • @click="scrollToSection(section.id)" 点击导航项时调用 scrollToSection 方法。
  • script 部分

    • data 中定义了 sections 数组存储章节数据,activeSection 存储当前激活的章节 ID。
    • mounted 钩子中添加滚动事件监听,并初始化激活章节。
    • beforeDestroy 钩子中移除滚动事件监听,避免内存泄漏。
    • handleScroll 方法监听滚动事件,根据滚动位置更新激活章节。
    • scrollToSection 方法用于滚动到指定章节。
  • style 部分

    • .content 定义内容区域的样式。
    • .floating-nav 定义底部浮动导航栏的样式。
    • .floating-nav div.active 定义激活导航项的样式。

使用说明

  1. 确保已经安装了 Node.js 和 npm。
  2. 按照上述步骤创建并编写代码。
  3. 在项目根目录下运行以下命令启动开发服务器:
npm run serve
  1. 打开浏览器,访问 http://localhost:8080,将看到一个包含内容区域和底部浮动导航栏的页面。
  2. 滚动页面时,导航栏的激活状态会根据滚动位置自动更新。
  3. 点击导航栏的项,页面会平滑滚动到相应的内容区域。

http://www.ppmy.cn/news/1570445.html

相关文章

七、深入学习TensorRT,Quantized

Working with Quantized Types(一) Quantization WorkflowsPost-training quantization (PTQ)Quantization-aware training (QAT)Quantization Modes 距离上一篇文章已经过去快一年了&#xff0c;这一年也真够忙的&#xff0c;也够累的&#xff0c;躺平吧又躺不平&#xff0c;赚…

C++----继承

一、继承的基本概念 本质&#xff1a;代码复用类关系建模&#xff08;是多态的基础&#xff09; class Person { /*...*/ }; class Student : public Person { /*...*/ }; // public继承 派生类继承基类成员&#xff08;数据方法&#xff09;&#xff0c;可以通过监视窗口检…

力扣-字符串-541 反转字符串Ⅱ

思路 和《反转字符串》的代码类似&#xff0c;只是每次处理2k个 代码 class Solution { public:string reverseStr(string s, int k) {int length s.length();int reverse 0;while(reverse < length){int left, right;if(reverse k < length){left reverse, right…

CSS Fonts(字体)

CSS Fonts(字体) 在网页设计中,字体是传达信息、增强用户体验和提升视觉效果的关键元素。CSS(层叠样式表)提供了丰富的工具和属性来控制网页中的字体样式。本文将详细探讨CSS字体相关的知识,包括字体的选择、样式设置、响应式设计以及跨浏览器兼容性等问题。 字体选择 …

ARM嵌入式学习--第十三天(I2C)

I2C --介绍 I2C&#xff08;Inter-intergrated Circuit 集成电路&#xff09;总线是Philips公司在八十年代初推出的一种串行、半双工的总线&#xff0c;主要用于近距离、低速的芯片之间的通信&#xff1b;I2C总线有俩根双向的信号线&#xff0c;一根数据线SDA用于收发数据&…

【鸿蒙开发】第二十四章 AI - Core Speech Kit(基础语音服务)

目录 1 简介 1.1 场景介绍 1.2 约束与限制 2 文本转语音 2.1 场景介绍 2.2 约束与限制 2.3 开发步骤 2.4 设置播报策略 2.4.1 设置单词播报方式 2.4.2 设置数字播报策略 2.4.3 插入静音停顿 2.4.4 指定汉字发音 2.5 开发实例 3 语音识别 3.1 场景介绍 3.2 约束…

java面试题-集合篇

Collection 1.Collection有哪些类&#xff1f; Java集合框架中的Collection接口是所有集合类的基础接口&#xff0c;定义了一些基本的集合操作&#xff0c;如添加元素、删除元素、判断是否包含某个元素等。常见的集合类包括List、Set和Queue。 List List接口定义了按照索引…

RabbitMQ 从入门到精通:从工作模式到集群部署实战(一)

#作者&#xff1a;闫乾苓 文章目录 RabbitMQ简介RabbitMQ与VMware的关系架构工作流程RabbitMQ 队列工作模式及适用场景简单队列模式&#xff08;Simple Queue&#xff09;工作队列模式&#xff08;Work Queue&#xff09;发布/订阅模式&#xff08;Publish/Subscribe&#xff…