vue 横向时间线组件
时间: 2023-08-04 21:06:19 浏览: 149
你可以尝试使用 Vue.js 开发时间线组件,具体实现可以参考以下步骤:
1. 安装必要的依赖:
```bash
npm install vue moment
```
2. 创建一个 TimelineItem.vue 组件,用于展示单个时间线条目的内容:
```vue
<template>
<div class="timeline-item">
<div class="timeline-item__date">{{ formattedDate }}</div>
<div class="timeline-item__content">
<slot></slot>
</div>
</div>
</template>
<script>
import moment from 'moment';
export default {
props: {
date: {
type: String,
required: true
}
},
computed: {
formattedDate() {
return moment(this.date).format('YYYY-MM-DD');
}
}
}
</script>
<style scoped>
.timeline-item {
position: relative;
padding: 1rem;
border-left: 2px solid #e5e5e5;
}
.timeline-item__date {
position: absolute;
top: 0;
left: -7rem;
width: 6rem;
text-align: right;
font-size: 0.8rem;
color: #999;
}
.timeline-item__content {
margin-left: 2rem;
}
</style>
```
3. 创建一个 Timeline.vue 组件,用于容纳多个时间线条目,并实现横向滚动:
```vue
<template>
<div class="timeline">
<div class="timeline__wrapper" ref="wrapper">
<div class="timeline__container" ref="container">
<slot></slot>
</div>
</div>
<div class="timeline__controls">
<button @click="scrollLeft">←</button>
<button @click="scrollRight">→</button>
</div>
</div>
</template>
<script>
export default {
methods: {
scrollLeft() {
this.$refs.wrapper.scrollBy({
left: -200,
behavior: 'smooth'
});
},
scrollRight() {
this.$refs.wrapper.scrollBy({
left: 200,
behavior: 'smooth'
});
}
}
}
</script>
<style scoped>
.timeline {
position: relative;
padding: 1rem 0;
overflow-x: auto;
}
.timeline__wrapper {
display: flex;
flex-direction: row;
align-items: flex-start;
}
.timeline__container {
display: flex;
flex-direction: row;
}
.timeline__controls {
position: absolute;
top: 0;
right: 0;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
width: 4rem;
height: 100%;
background-color: rgba(255, 255, 255, 0.8);
z-index: 1;
}
.timeline__controls button {
border: none;
background-color: transparent;
font-size: 1.2rem;
cursor: pointer;
outline: none;
}
</style>
```
4. 在需要展示时间线的页面中使用 Timeline 和 TimelineItem 组件:
```vue
<template>
<div>
<h1>My Timeline</h1>
<Timeline>
<TimelineItem date="2021-01-01">
<h3>Event A</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</TimelineItem>
<TimelineItem date="2021-02-01">
<h3>Event B</h3>
<p>Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</TimelineItem>
<TimelineItem date="2021-03-01">
<h3>Event C</h3>
<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</TimelineItem>
<TimelineItem date="2021-04-01">
<h3>Event D</h3>
<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
</TimelineItem>
</Timeline>
</div>
</template>
<script>
import Timeline from './Timeline.vue';
import TimelineItem from './TimelineItem.vue';
export default {
components: {
Timeline,
TimelineItem
}
}
</script>
```
通过以上步骤,你就可以创建一个简单的横向时间线组件,用户可以通过左右滚动查看不同时间点的事件。你可以根据自己的需求对组件进行进一步的定制和优化。
阅读全文