Vue 3 实现左侧列表点击跳转滚动到右侧对应区域的功能

ops/2024/10/18 9:17:35/

使用 Vue 3 实现左侧列表点击跳转到右侧对应区域的功能

1. 引言

在这篇博客中,我们将展示如何使用 Vue 3 实现一个简单的页面布局,其中左侧是一个列表,点击列表项时,右侧会平滑滚动到对应的内容区域。这种布局在很多应用场景中都非常常见,比如目录导航、问答系统等。

2. 效果图

在这里插入图片描述
在这里插入图片描述

3. 代码示例

demo.vue
<template><div class="container"><div class="sidebar"><ul><li v-for="item in items" :key="item.id" @click="jump(item)">{{ item.name }}</li></ul></div><div class="content"><divv-for="item in items":key="item.id":ref="el => setCombinationRef(el, `comb-${item.id}`)"class="content-item"><h2>{{ item.name }}</h2><p>{{ item.description }}</p></div></div></div>
</template><script setup>javascript">
import { reactive, nextTick } from 'vue';// 示例数据
const items = [{ id: 1, name: 'Item 1', description: 'Description for Item 1' },{ id: 2, name: 'Item 2', description: 'Description for Item 2' },{ id: 3, name: 'Item 3', description: 'Description for Item 3' },{ id: 4, name: 'Item 4', description: 'Description for Item 4' },{ id: 5, name: 'Item 5', description: 'Description for Item 5' },{ id: 6, name: 'Item 6', description: 'Description for Item 6' },{ id: 7, name: 'Item 7', description: 'Description for Item 7' },{ id: 8, name: 'Item 8', description: 'Description for Item 8' },{ id: 9, name: 'Item 9', description: 'Description for Item 9' },{ id: 10, name: 'Item 10', description: 'Description for Item 10' },{ id: 11, name: 'Item 11', description: 'Description for Item 11' },{ id: 12, name: 'Item 12', description: 'Description for Item 12' },{ id: 13, name: 'Item 13', description: 'Description for Item 13' },{ id: 14, name: 'Item 14', description: 'Description for Item 14' },{ id: 15, name: 'Item 15', description: 'Description for Item 15' },{ id: 16, name: 'Item 16', description: 'Description for Item 16' },{ id: 17, name: 'Item 17', description: 'Description for Item 17' },{ id: 18, name: 'Item 18', description: 'Description for Item 18' },{ id: 19, name: 'Item 19', description: 'Description for Item 19' },{ id: 20, name: 'Item 20', description: 'Description for Item 20' }
];// 使用reactive创建一个响应式的对象来存储refs
const combinationRefs = reactive({});// 动态设置ref
const setCombinationRef = (element, id) => {if (element) {combinationRefs[id] = element;}
};// 提供一个方法来获取特定的ref
const getCombinationRef = id => {return combinationRefs[id];
};// 跳转到对应小题的位置
const jump = async item => {const el = getCombinationRef(`comb-${item.id}`);if (el) {await nextTick();el.scrollIntoView({ behavior: 'smooth' });}
};
</script><style scoped>
.container {display: flex;
}.sidebar {width: 200px;border-right: 1px solid #ccc;padding: 10px;
}.sidebar ul {list-style: none;padding: 0;
}.sidebar li {cursor: pointer;padding: 5px 0;
}.sidebar li:hover {background-color: #f0f0f0;
}.content {flex: 1;padding: 10px;
}.content-item {margin-bottom: 20px;padding: 10px;border: 1px solid #ccc;
}
</style>
实现跳转功能

在点击左侧列表项时,我们需要跳转到右侧对应的内容区域。为此,我们需要:

  • 使用 ref 获取每个内容区域的 DOM 元素。
  • 在点击事件中调用 scrollIntoView 方法,实现平滑滚动。
// 使用reactive创建一个响应式的对象来存储refs
const combinationRefs = reactive({});// 动态设置ref
const setCombinationRef = (element, id) => {if (element) {combinationRefs[id] = element;}
};// 提供一个方法来获取特定的ref
const getCombinationRef = id => {return combinationRefs[id];
};// 跳转到对应小题的位置
const jump = async item => {const el = getCombinationRef(`comb-${item.id}`);if (el) {await nextTick();el.scrollIntoView({ behavior: 'smooth' });}
};

结论

通过以上步骤,我们成功实现了一个简单的 Vue 3 页面布局,左侧是一个列表,点击列表项时,右侧会平滑滚动到对应的内容区域。这种布局和滚动效果在实际开发中非常常见,希望这篇博客对你有所帮助。


http://www.ppmy.cn/ops/84765.html

相关文章

华为ensp中ISIS原理与配置(超详细)

isis原理与配置 8-20字节&#xff1b; 地址组成&#xff1a;area id&#xff0c;system id&#xff0c;set三部分组成&#xff1b; system id占6个字节&#xff1b;sel占一个&#xff0c;剩下的为area id区域号&#xff1b; system id 唯一&#xff0c; 一般将router id 配…

分享一个Springer模板关于论文作者和单位信息的修改范例,以及Applied Intelligence期刊latex模板的下载链接

在这篇文章中&#xff0c;我写一些关于解决springer期刊提供的LaTex模板参考文献格式为作者年份时的顺序问题以及如何在正文中将参考文献格式引用成[1]这种数字格式类似的经验&#xff0c;该篇帖子里还分享了一个大佬关于springer模板完整的修改流程&#xff0c;有需要的伙伴可…

数据结构经典测试题1

1. char a101; int sum200; a27;suma; printf("%d\n",sum); 上述代码运行结果是什么呢&#xff1f; A: 327 B: 99 C: 328 D: 72 答案为D。 char为有符号类型&#xff0c;占1个字节&#xff0c;也就是8位&#xff0c;其中最高位是符号位&#xff0c;取值范围为-…

HTML(五)——HTML区块,布局

HTML区块 HTML可以通过 <div> 和 <span>将元素组合起来&#xff0c;可以来布局&#xff0c;就是盒子&#xff0c;div是块级盒子&#xff0c;里面 可以放任何东西&#xff0c;span里面装的是文本 HTML 区块元素 大多数 HTML 元素被定义为块级元素或内联元素。 实…

nginx漏洞修复 ngx_http_mp4_module漏洞(CVE-2022-41742)【低可信】 nginx版本升级

风险描述&#xff1a; Nginx 是一款轻量级的Web服务器、反向代理服务器。 Nginx 的受影响版本中的ngx _http_mp4_module模块存在内存越界写入漏洞&#xff0c;当在配置中使用 mp4 directive时&#xff0c;攻击者可利用此漏洞使用使用ngx_http_mp4_module模块处理特制的音频或视…

紫龙游戏服务器面试

1.C# 引用类型和值类型的区别 2.C# 防止内存碎片有哪些方法 3.由于会发生GC会导致内存压缩&#xff0c;那么如果C# 和C共同访问的内存在发生内存压缩时&#xff0c;C是否还能找到之前访问的内存 使用堆外内存 4.C# GC算法 5.C# event 是线程安全的吗 event 和 action…

npm上传自己的包以及发布过程遇到的问题

大家好&#xff0c;我是前端追寻路上的【酱酱仔】 作为在前端领域不断探索的一员&#xff0c;在此记录开发中遇到的问题&#xff0c;如果你也遇到了相同的问题&#xff0c;希望本文对你有帮助。 前提&#xff1a;本文涉及的命令都是在要发布的包的根目录下执行的&#xff0c;在…

【vueUse库Time模块各函数简介及使用方法--全篇】

vueUse库是一个专门为Vue打造的工具库,提供了丰富的功能,包括监听页面元素的各种行为以及调用浏览器提供的各种能力等。其中的Browser模块包含了一些实用的函数,以下是这些函数的简介和使用方法: vueUse库Sensors模块各函数简介及使用方法 vueUseTime函数1. useDateFormat…