当前位置:首页 > VUE

vue如何实现timeline

2026-01-19 08:01:40VUE

Vue 实现 Timeline 的方法

使用第三方库(如 vis-timeline)

安装 vis-timeline 库:

npm install vis-timeline vis-data

在 Vue 组件中使用:

vue如何实现timeline

<template>
  <div ref="timeline"></div>
</template>

<script>
import { Timeline } from 'vis-timeline';
import 'vis-timeline/styles/vis-timeline-graph2d.css';

export default {
  mounted() {
    const container = this.$refs.timeline;
    const items = [
      { id: 1, content: '事件1', start: '2023-01-01' },
      { id: 2, content: '事件2', start: '2023-02-01' }
    ];
    new Timeline(container, items);
  }
};
</script>

自定义实现 Timeline

通过 Vue 组件和 CSS 自定义时间线:

vue如何实现timeline

<template>
  <div class="timeline">
    <div v-for="(item, index) in items" :key="index" class="timeline-item">
      <div class="timeline-dot"></div>
      <div class="timeline-content">
        <h3>{{ item.title }}</h3>
        <p>{{ item.date }}</p>
        <p>{{ item.description }}</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { title: '事件1', date: '2023-01-01', description: '描述1' },
        { title: '事件2', date: '2023-02-01', description: '描述2' }
      ]
    };
  }
};
</script>

<style>
.timeline {
  position: relative;
  padding-left: 50px;
}
.timeline-item {
  position: relative;
  margin-bottom: 30px;
}
.timeline-dot {
  position: absolute;
  left: -25px;
  top: 0;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #42b983;
}
.timeline-content {
  padding: 10px;
  border-left: 2px solid #42b983;
}
</style>

使用 Vuetify 的 v-timeline 组件

如果项目使用 Vuetify,可以直接使用内置组件:

<template>
  <v-timeline>
    <v-timeline-item
      v-for="(item, index) in items"
      :key="index"
      :color="item.color"
    >
      <template v-slot:opposite>
        {{ item.date }}
      </template>
      <v-card>
        <v-card-title>{{ item.title }}</v-card-title>
        <v-card-text>{{ item.description }}</v-card-text>
      </v-card>
    </v-timeline-item>
  </v-timeline>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { title: '事件1', date: '2023-01-01', description: '描述1', color: 'red' },
        { title: '事件2', date: '2023-02-01', description: '描述2', color: 'blue' }
      ]
    };
  }
};
</script>

使用 Element UI 的时间线

如果项目使用 Element UI:

<template>
  <el-timeline>
    <el-timeline-item
      v-for="(item, index) in items"
      :key="index"
      :timestamp="item.date"
    >
      {{ item.content }}
    </el-timeline-item>
  </el-timeline>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { date: '2023-01-01', content: '事件1' },
        { date: '2023-02-01', content: '事件2' }
      ]
    };
  }
};
</script>

每种方法适用于不同的场景和需求,第三方库提供更丰富的功能,自定义实现更灵活,UI 框架内置组件则更简单易用。

标签: 如何实现vue
分享给朋友:

相关文章

vue设计与实现下载

vue设计与实现下载

vue设计与实现电子书下载 《Vue.js设计与实现》是一本深入解析Vue.js框架原理的书籍,由霍春阳(HcySunYang)撰写。以下是获取该资源的常见途径: 正版购买渠道 京东、当当、天猫等…

vue实现打印二维码

vue实现打印二维码

安装二维码生成库 使用 qrcode 或 vue-qrcode 库生成二维码。通过 npm 安装: npm install qrcode vue-qrcode 生成二维码 在 Vue 组件中引入…

vue实现同步

vue实现同步

Vue 实现同步的方法 在 Vue 中实现同步操作通常涉及数据绑定、状态管理和异步操作的处理。以下是几种常见的方法: 使用计算属性(Computed Properties) 计算属性基于响应式依赖进…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue实现hover

vue实现hover

Vue 实现 hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式实现,包括使用 CSS、Vue 指令或事件监听。以下是几种常见的方法: 使用 CSS :hover 伪类 通…

vue插件实现

vue插件实现

Vue 插件实现方法 Vue 插件是一种向 Vue 应用程序添加全局功能的机制。插件可以包含全局指令、过滤器、混入、实例方法等。 插件的基本结构 一个 Vue 插件通常是一个对象或函数,需要暴露一个…