当前位置:首页 > 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 数据:

vue如何实现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
分享给朋友:

相关文章

vue 方法实现

vue 方法实现

在 Vue 中,方法的实现通常通过 methods 选项完成。以下是 Vue 方法实现的核心要点和示例: 基本方法定义 在 Vue 组件中定义方法时,需将函数声明放在 methods 对象内。这些方…

vue插槽实现

vue插槽实现

插槽的基本概念 Vue插槽(Slot)是一种内容分发机制,允许父组件向子组件传递模板片段,子组件通过<slot>标签定义接收位置。插槽的核心作用是增强组件的灵活性和复用性。 默认插槽 子…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…

vue jwt实现

vue jwt实现

Vue JWT 实现方法 安装依赖 确保项目中安装了 jsonwebtoken(后端)和 axios(前端)。若使用 Vue 3,可搭配 vue-router 和 pinia(或 vuex)管理状态。…

vue 实现原理

vue 实现原理

Vue 实现原理 Vue.js 是一个渐进式 JavaScript 框架,其核心实现原理主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是 Vue 实现原理的核心要点: 响应式系统 Vu…

vue实现http拦截

vue实现http拦截

Vue 实现 HTTP 拦截 在 Vue 项目中,通常通过 Axios 实现 HTTP 拦截功能,用于统一处理请求和响应。以下是具体实现方法: 安装 Axios 通过 npm 或 yarn 安装 A…