当前位置:首页 > VUE

vue如何实现timeline

2026-01-19 08:01:40VUE

Vue 实现 Timeline 的方法

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

安装 vis-timeline 库:

npm install vis-timeline vis-data

在 Vue 组件中使用:

<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 自定义时间线:

<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如何实现timeline

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

相关文章

vue实现atm

vue实现atm

Vue实现ATM机模拟功能 使用Vue.js实现ATM机模拟功能需要结合组件化开发、状态管理以及用户交互逻辑。以下是关键实现步骤: 核心组件结构 创建以下Vue组件构建ATM界面: Car…

vue实现购物按钮

vue实现购物按钮

Vue 购物按钮实现方法 基础按钮实现 使用 Vue 的模板语法创建基础按钮组件,绑定点击事件处理购物逻辑: <template> <button @click="addToC…

vue表单实现搜索

vue表单实现搜索

实现Vue表单搜索功能 在Vue中实现表单搜索功能通常涉及表单绑定、事件处理和搜索逻辑。以下是具体实现方法: 数据绑定与表单结构 使用v-model实现表单与Vue数据的双向绑定: <t…

vue怎么实现选中删除

vue怎么实现选中删除

实现选中删除功能的方法 在Vue中实现选中删除功能通常涉及以下几个关键步骤: 数据绑定与选中状态管理 使用v-model或v-for指令绑定列表数据,结合复选框或单选按钮实现选中状态管理。例如:…

vue公共列表的实现

vue公共列表的实现

实现 Vue 公共列表组件 公共列表组件通常用于展示重复结构的数据,例如商品列表、用户列表等。以下是一个典型的实现方式: 基础实现 创建可复用的列表组件 List.vue: <templ…

vue穿梭框组件实现

vue穿梭框组件实现

实现基础穿梭框组件 在Vue中实现穿梭框组件,可以使用<el-transfer>(Element UI)或手动实现。以下是手动实现的核心逻辑: <template> &l…