vue2中el-timeline横向且到一定长度拐弯到第二行切、又箭头拐弯效果
时间: 2025-06-29 14:01:00 浏览: 16
### 实现 Vue2 中 Element UI 的时间线组件 (el-timeline) 横向布局并带换行和箭头拐弯效果
为了实现这一功能,可以通过自定义 CSS 和 HTML 结构来扩展 `el-timeline` 组件的功能。具体方法如下:
#### 自定义样式与结构
由于默认的 `el-timeline` 是垂直排列的,要实现横向布局并在特定长度后换行的效果,需要重写部分样式。
```html
<template>
<div class="custom-timeline">
<el-row :gutter="20" type="flex" justify="start" style="flex-wrap: wrap;">
<el-col v-for="(item, index) in items" :key="index" :span="8">
<el-card shadow="hover" style="margin-bottom: 1rem; position:relative;">
<!-- 时间轴点 -->
<i class="timeline-dot" :class="{ 'last-item': isLastItem(index) }"></i>
{{ item.content }}
</el-card>
<!-- 连接线 -->
<div v-if="!isLastItem(index)" class="line-container">
<div class="horizontal-line"></div>
<div class="arrow-bend"></div>
</div>
</el-col>
</el-row>
</div>
</template>
<script>
export default {
data() {
return {
items: [
{ content: "事件一" },
{ content: "事件二" },
{ content: "事件三" },
// 更多条目...
]
};
},
methods: {
isLastItem(index) {
return index === this.items.length - 1;
}
}
};
</script>
<style scoped>
.custom-timeline .el-card {
display: flex;
align-items: center;
}
.timeline-dot {
width: 16px;
height: 16px;
background-color: #409EFF;
border-radius: 50%;
position: absolute;
top: calc(50% - 8px);
left: -17px;
z-index: 1;
}
.last-item::after {
content: "";
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
width: 0;
height: 0;
border-left: 8px solid transparent;
border-right: 8px solid transparent;
border-top: 8px solid #409EFF;
}
.line-container {
position: relative;
margin-left: -17px;
}
.horizontal-line {
width: calc((100vw / 3) + 20px); /* 假设每列宽度 */
height: 2px;
background-color: #e4e7ed;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.arrow-bend {
width: 16px;
height: 16px;
background-color: #e4e7ed;
clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%);
position: absolute;
top: 50%;
left: 100%;
transform: translateY(-50%) rotate(45deg);
}
</style>
```
此代码片段展示了如何创建一个具有水平方向的时间线,并在到达容器边界时自动折行至下一行。每一项之间通过线条连接,在转角处添加了一个指向下一个项目的箭头效果[^1]。
对于更复杂的场景或需求更高的定制化程度,建议考虑开发独立的时间线组件或将现有插件作为基础进行修改以满足特殊要求。
阅读全文