当前位置:首页 > 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 中实现同步操作通常涉及数据绑定、状态管理和异步操作的处理。以下是几种常见的方法: 使用计算属性(Computed Properties) 计算属性基于响应式依赖进…

vue页面实现

vue页面实现

Vue 页面实现方法 创建 Vue 组件 使用 Vue 单文件组件(SFC)方式创建页面,文件扩展名为 .vue。一个基本的 Vue 组件包含三个部分:模板(template)、脚本(script)和…

vue实现ajax

vue实现ajax

Vue 中实现 AJAX 的几种方法 在 Vue 中实现 AJAX 请求可以通过原生 JavaScript 的 XMLHttpRequest、第三方库如 axios 或 fetch API 来完成。以…

vue实现加减

vue实现加减

Vue 实现加减功能 在 Vue 中实现加减功能可以通过数据绑定和事件监听来完成。以下是一个简单的实现方法: 模板部分 <template> <div> <…

vue toast实现

vue toast实现

Vue Toast 实现方法 使用第三方库(推荐) 安装 vue-toastification 库,这是一个功能丰富且轻量级的 Vue Toast 插件。 npm install vue-toast…

vue源码实现

vue源码实现

Vue 源码实现解析 Vue.js 的核心实现可以分为响应式系统、虚拟 DOM、模板编译、组件化等几个关键部分。以下是对这些核心机制的详细解析。 响应式系统 Vue 的响应式系统基于 Object.…