当前位置:首页 > VUE

vue如何实现timeline

2026-02-20 00:22:28VUE

Vue 实现 Timeline 的方法

使用第三方库(如 vue-timeline

安装 vue-timeline 库:

npm install vue-timeline --save

在组件中引入并使用:

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

<script>
import { Timeline, TimelineItem } from 'vue-timeline';

export default {
  components: {
    Timeline,
    TimelineItem
  },
  data() {
    return {
      items: [
        { content: '事件 1' },
        { content: '事件 2' },
        { content: '事件 3' }
      ]
    };
  }
};
</script>

自定义实现 Timeline

通过 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.description }}</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { title: '事件 1', description: '描述内容 1' },
        { title: '事件 2', description: '描述内容 2' },
        { title: '事件 3', description: '描述内容 3' }
      ]
    };
  }
};
</script>

<style>
.timeline {
  position: relative;
  padding-left: 20px;
}

.timeline-item {
  position: relative;
  padding-bottom: 20px;
}

.timeline-dot {
  position: absolute;
  left: -10px;
  top: 0;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background-color: #42b983;
}

.timeline-content {
  margin-left: 30px;
}

.timeline-item:not(:last-child)::after {
  content: '';
  position: absolute;
  left: 0;
  top: 20px;
  height: 100%;
  width: 2px;
  background-color: #e0e0e0;
}
</style>

使用 Vuetify 的 Timeline 组件

如果项目使用 Vuetify,可以直接使用其内置的 v-timeline 组件:

<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: [
        { date: '2023-01-01', title: '事件 1', description: '描述内容 1', color: 'red' },
        { date: '2023-02-01', title: '事件 2', description: '描述内容 2', color: 'blue' },
        { date: '2023-03-01', title: '事件 3', description: '描述内容 3', color: 'green' }
      ]
    };
  }
};
</script>

动态加载 Timeline 数据

结合 API 动态加载 Timeline 数据:

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

<script>
export default {
  data() {
    return {
      timelineData: []
    };
  },
  async created() {
    try {
      const response = await fetch('https://api.example.com/timeline');
      this.timelineData = await response.json();
    } catch (error) {
      console.error('加载数据失败:', error);
    }
  }
};
</script>

vue如何实现timeline

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

相关文章

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

vue compile 实现

vue compile 实现

Vue 编译实现原理 Vue 的编译过程将模板字符串转换为渲染函数,主要分为解析、优化和代码生成三个阶段。 解析阶段(Parse) 将模板字符串转换为抽象语法树(AST)。Vue 使用正则表达式和…

vue 实现视频

vue 实现视频

Vue 实现视频播放功能 使用 Vue 实现视频播放功能可以通过 HTML5 的 <video> 标签或第三方库(如 video.js)来实现。以下是两种常见的方法: 使用 HTML5…

vue diff实现

vue diff实现

Vue Diff 算法实现原理 Vue 的虚拟 DOM Diff 算法(即 patch 过程)是其高效渲染的核心,主要通过对比新旧虚拟节点的差异,最小化 DOM 操作。以下是关键实现要点: 同层级比…

vue搜索实现

vue搜索实现

Vue 搜索功能实现方法 基于计算属性的搜索 在 Vue 中可以通过计算属性实现简单的搜索功能。这种方式适合小型数据集,利用 JavaScript 的 filter 方法进行筛选。 data() {…

vue实现心电图

vue实现心电图

Vue 实现心电图效果 在 Vue 中实现心电图效果通常需要结合 Canvas 或 SVG 进行动态绘图,模拟心电图数据的实时变化。以下是两种实现方式的核心思路和代码示例: 使用 Canvas…