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

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

使用 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>
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 页面布局,左侧是一个列表,点击列表项时,右侧会平滑滚动到对应的内容区域。这种布局和滚动效果在实际开发中非常常见,希望这篇博客对你有所帮助。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 高速板开源项目学习(二)
  • 【Node.js基础05】包的理解与使用
  • BUUCTF [MRCTF2020]Ezpop
  • LLM 大语言模型显存消耗估计与计算
  • C语言 —— 函数
  • 如何在Linux上安装配置RabbitMQ消息队列
  • json的数据结构
  • 测试用例的设计方法
  • AOP面向切面编程和log4j的使用(Java版)
  • 多列传感器数据多项式拟合python脚本
  • 斯坦福UE4 C++课学习补充 14:UMG-优化血量条
  • spring常用注解有哪些
  • YOLOv5改进 | 注意力机制 | 二阶注意力网络来进行单图像超分辨率【附网盘完整代码】
  • 力扣SQL 最后一个能进入巴士的人 自连接
  • 【爱上C++】list用法详解、模拟实现
  • ➹使用webpack配置多页面应用(MPA)
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • maya建模与骨骼动画快速实现人工鱼
  • 转载:[译] 内容加速黑科技趣谈
  • [地铁译]使用SSD缓存应用数据——Moneta项目: 低成本优化的下一代EVCache ...
  • # Redis 入门到精通(九)-- 主从复制(1)
  • #AngularJS#$sce.trustAsResourceUrl
  • #前后端分离# 头条发布系统
  • (2024.6.23)最新版MAVEN的安装和配置教程(超详细)
  • (Java入门)学生管理系统
  • (二)springcloud实战之config配置中心
  • (附源码)ssm考试题库管理系统 毕业设计 069043
  • (六)激光线扫描-三维重建
  • (学习日记)2024.01.19
  • (一)、软硬件全开源智能手表,与手机互联,标配多表盘,功能丰富(ZSWatch-Zephyr)
  • .class文件转换.java_从一个class文件深入理解Java字节码结构
  • .net framework 4.0中如何 输出 form 的name属性。
  • .NET技术成长路线架构图
  • .php结尾的域名,【php】php正则截取url中域名后的内容
  • /bin/bash^M: bad interpreter: No such file ordirectory
  • @RequestBody与@RequestParam
  • [ 转载 ] SharePoint 资料
  • [.NET 即时通信SignalR] 认识SignalR (一)
  • [2013][note]通过石墨烯调谐用于开关、传感的动态可重构Fano超——
  • [2021 蓝帽杯] One Pointer PHP
  • [8481302]博弈论 斯坦福game theory stanford week 1
  • [BZOJ5250][九省联考2018]秘密袭击(DP)
  • [C#7] 1.Tuples(元组)
  • [C#数据加密]——MD5、SHA、AES、RSA
  • [C++]——带你学习类和对象
  • [C++]命名空间等——喵喵要吃C嘎嘎
  • [CF494C]Helping People
  • [CISCN2019 华东北赛区]Web2
  • [JS]Math.random()随机数的二三事
  • [Linux] PXE批量装机
  • [Linux安全运维] Nginx安装部署以及LNMP框架搭建保姆级教程
  • [MRCTF2020]PixelShooter
  • [NAND Flash 7.1] 闪存系统性能优化方向集锦?AC timing? Cache? 多路并发?
  • [Oh My C++ Diary]Main函数参数argc,argv如何传入
  • [python] 之 装饰器