vue写一个水平时间轴
时间: 2024-05-01 13:23:01 浏览: 9
好的,下面是一个简单的水平时间轴的Vue组件示例:
```html
<template>
<div class="timeline">
<div class="timeline-inner">
<div
v-for="(event, index) in events"
:key="index"
class="timeline-event"
:class="{ 'timeline-left': (index % 2 === 0), 'timeline-right': (index % 2 === 1) }"
>
<div class="timeline-event-circle"></div>
<div class="timeline-event-content">
<h3>{{ event.title }}</h3>
<p>{{ event.description }}</p>
</div>
<div class="timeline-event-date">{{ event.date }}</div>
</div>
</div>
</div>
</template>
<script>
export default {
props: {
events: {
type: Array,
required: true
}
}
}
</script>
<style scoped>
.timeline {
position: relative;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.timeline-inner {
position: relative;
width: 80%;
height: 6px;
background-color: #ddd;
}
.timeline-event {
position: absolute;
top: -20px;
width: 300px;
display: flex;
flex-direction: column;
align-items: center;
}
.timeline-left {
left: calc(20% - 150px);
}
.timeline-right {
right: calc(20% - 150px);
}
.timeline-event-circle {
width: 30px;
height: 30px;
border-radius: 50%;
background-color: #333;
margin-bottom: 10px;
}
.timeline-event-content {
text-align: center;
}
.timeline-event-date {
margin-top: 5px;
font-size: 0.8rem;
color: #999;
}
</style>
```
上述代码中,我们通过`v-for`指令遍历传入的时间轴事件数组,并根据事件在数组中的下标奇偶性来决定事件是左对齐还是右对齐。同时,我们还使用了一些CSS样式来实现时间轴的布局和样式。
你可以通过传入一个包含时间轴事件的数组给该组件来使用它:
```html
<template>
<div>
<timeline :events="events" />
</div>
</template>
<script>
import Timeline from '@/components/Timeline.vue'
export default {
components: {
Timeline
},
data () {
return {
events: [
{ title: 'Event 1', description: 'Description of event 1', date: '2022-01-01' },
{ title: 'Event 2', description: 'Description of event 2', date: '2022-02-01' },
{ title: 'Event 3', description: 'Description of event 3', date: '2022-03-01' },
{ title: 'Event 4', description: 'Description of event 4', date: '2022-04-01' },
{ title: 'Event 5', description: 'Description of event 5', date: '2022-05-01' },
{ title: 'Event 6', description: 'Description of event 6', date: '2022-06-01' }
]
}
}
}
</script>
```
以上代码将会渲染出一个包含6个事件的时间轴。你可以根据需要修改事件数组中的内容和样式来适应你的业务需求。