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

前端实践:打造高度可定制的Vue3时间线组件——图标、节点与连接线的个性化配置

前言
在项目开发中,我需要实现一个支持自定义图标、节点颜色和辅助线的时间轴组件。经过多方搜索,发现现有方案都无法完全满足需求,于是决定自行开发。现将实现过程记录下来,希望能为遇到类似需求的开发者提供参考。

效果预览(以工作经历&教育经历为例)
在这里插入图片描述
核心特性

  1. 完全可定制:图标、节点颜色、连接线独立配置
  2. 响应式布局:完美适配不同内容长度

组件使用指南

<template><stepBar type="work" :stepList="handelData(infoDetail.resumeWorkExpList, 'work')" /><stepBar class="mt-8" type="education" :stepList="handelData(infoDetail.resumeEduExpList, 'education')" />
</template>
import educationIcon from '@/assets/img/ai/education_icon.png'
import companyIcon from '@/assets/img/ai/company_icon.png'
import { stepBar } from './Components'
const educationIconImg = educationIcon
const companyIconImg  = companyIcon
const handelData = (data, type) => {let stepList = []if (type === 'work') {data.forEach((item) => {const timeStr = [];if (item.workStartDate) timeStr.push(item.workStartDate);if (item.workEndDate) timeStr.push(item.workEndDate);const parts = [];if (item.workCompany) parts.push(item.workCompany);if (item.workJobName) parts.push(item.workJobName);stepList.push({icon: companyIconImg,time: timeStr.join(' - '),desc:  parts.join(' · '),linColor: '#7AC3FF',dotColor: '#006EF0'})})} else {data.forEach((item) => {const timeStr = [];if (item.eduStartDate) timeStr.push(item.eduStartDate);if (item.eduEndDate) timeStr.push(item.eduEndDate);const parts = [];if (item.schoolName) parts.push(item.schoolName);if (item.speciality) parts.push(item.speciality);const educationLabel = translateDict(item.education, dictStore.dicts['topEduDegree']);parts.push(educationLabel);stepList.push({icon: educationIconImg,time: timeStr.join(' - '),desc: parts.join(' · '),linColor: '#B9E4D6',dotColor: '#19B383'})})}return stepList
}

组件实现解析
核心代码结构

<template><div class="timeline-container"><div v-for="(item, index) in stepList" :key="index" class="timeline-item":class="{ 'last-item': index === stepList.length - 1 }"><img :src="item.icon" v-if="index === 0" class="current-icon"/><div v-else class="timeline-dot" :style="{ background:item.dotColor}"></div><div class="text-content ml-12"><span class="time-label">{{ item.time }}</span><span class="desc-text">{{ item.desc }}</span></div><div v-if="index !== stepList.length - 1" class="timeline-line" :style="{ background:item.linColor}"></div></div></div>
</template><script setup>
const props = defineProps({type: {type: String,default: 'work'},stepList: {type: Array,default: () => []}
})</script><style lang="scss" scoped>
.timeline-container {width: 100%;padding-left: 3px;box-sizing: border-box;
}.timeline-item {position: relative;margin-bottom: 8px;min-height: 20px;/* background: red; */
}
.text-content{display: flex;}
.time-label {font-size: 14px;font-weight: normal;line-height: 20px;letter-spacing: 0px;color: #606266;flex-shrink: 0;min-width: 118px;
}.desc-text {font-size: 14px;font-weight: normal;line-height: 20px;letter-spacing: 0px;color: #303133;
}.timeline-dot {position: absolute;left: -3px;top: 5px;width: 6px;height: 6px;border-radius: 50%;z-index: 2;
}
.current-icon{position: absolute;left: -8px;top: -2px;width: 16px;height: 15px;
}
.timeline-line {position: absolute;left: -1px;top: 11px;bottom: -15px;width: 2px;
}.last-item {margin-bottom: 0;
}
</style>

相关文章:

  • Keil5 MDK 安装教程
  • 医学影像系统的集成与工作流优化
  • 数据结构中的高级排序算法
  • 【C++设计模式之Decorator装饰模式】
  • PPO算法:一种先进的强化学习策略
  • WeakAuras Lua Script ICC (BarneyICC)
  • Python中列表(list)知识详解(2)和注意事项以及应用示例
  • lua 作为嵌入式设备的配置语言
  • java加强 -stream流
  • spark数据压缩
  • Spark之搭建Yarn模式
  • 一文了解 HTTP Content-Type:从基础到实战
  • 魔改离线VLLM
  • Adobe DC 2025安装教程
  • Android usb网络共享详解
  • 【华为HCIP | 华为数通工程师】821—多选解析—第二十四页
  • AI数字人实现原理
  • 动态多因子策略
  • 【轻松学 C:编程小白的大冒险】— 16 函数的定义与调用
  • 速查 Linux 常用指令 II
  • 人民日报民生观:转人工客服,怎么这么难?
  • 订婚不等于性同意!山西订婚强奸案入选最高法案例
  • 刘永明|在从普及到提高中发展新大众文艺
  • 图讯丨习近平出席中国-拉美和加勒比国家共同体论坛第四届部长级会议开幕式
  • 三大股份制银行入局AIC,一级市场再迎“活水”
  • 刘元春在《光明日报》撰文:以法治护航民营经济高质量发展