vue实现横向时间轴组件

前言:项目中有需要用到横向时间轴,网上大部分组件不满足 功能需要,于是自己写了一个。先上简单的demo。

功能:

  1. 默认获取初始数据显示对应的时间轴和时间点。
  2. 当超出屏幕后,滑动滚动条加载更多页,类似分页加载。
  3. 某个大的时间轴节点鼠标放入需要显示相关信息。
  4. 某两个大节点之间会有子节点出现。
  5. 每个子节点会有对应的子节点详情内容展示,无详情内容介绍的只展示子节点。

效果图

vue时间轴视频效果

在这里插入图片描述

代码

Timeline组件封装

<template>
  <ul class="timeline-wrapper" @scroll="scrollEvent">
    <li class="timeline-item" v-for="item in timelineList" :key="item.id">
      <div class="timeline-box">
        <div class="out-circle">
          <div class="in-circle"></div>
          <div class="timeline-date">
            <el-popover
              placement="bottom"
              title="标题"
              width="200"
              trigger="hover"
              :content="item.content"
            >
              <el-button type="text" slot="reference" class="father-text">{
   {
   
                item.date
              }}</el-button>
            </el-popover>
          </div>
        </div>
        <div
          class="long-line"
          v-show="item.isShow"
          :style="`width:${
     
            item.children ? (item.children.length + 1) * 100 : 1 * 100
          }px`"
        >
          <div
            v-for="(subItem, index) in item.children"
            :key="subItem.id"
            class="sub-item-box"
          >
            <span>{
   {
    subItem.name + ":" + subItem.num }}</span>
            <!-- 根据奇数偶数来判断向上还是向下 -->
            <div
              :class="`sub-line-box ${
     
                index % 2 == 0 ? 'top-line-box' : 'bottom-line-box'
              }`"
              v-show="subItem.content"
            >
              <div
                :class="`children-line-box ${
     
                  index % 2 == 0 ? 'top-line' : 'bottom-line'
                }`"
              ></div>
              <div
                :class="`children-box ${
     
                  index % 2 == 0 ? 'top-children-box' : 'bottom-children-box'
                }`"
              >
                {
   {
    subItem.content }}
              </div>
            </div>
          </div>
        </div>
    
  • 17
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以尝试使用 Vue.js 开发时间线组件,具体实现可以参考以下步骤: 1. 安装必要的依赖: ```bash npm install vue moment ``` 2. 创建一个 TimelineItem.vue 组件,用于展示单个时间线条目的内容: ```vue <template> <div class="timeline-item"> <div class="timeline-item__date">{{ formattedDate }}</div> <div class="timeline-item__content"> <slot></slot> </div> </div> </template> <script> import moment from 'moment'; export default { props: { date: { type: String, required: true } }, computed: { formattedDate() { return moment(this.date).format('YYYY-MM-DD'); } } } </script> <style scoped> .timeline-item { position: relative; padding: 1rem; border-left: 2px solid #e5e5e5; } .timeline-item__date { position: absolute; top: 0; left: -7rem; width: 6rem; text-align: right; font-size: 0.8rem; color: #999; } .timeline-item__content { margin-left: 2rem; } </style> ``` 3. 创建一个 Timeline.vue 组件,用于容纳多个时间线条目,并实现横向滚动: ```vue <template> <div class="timeline"> <div class="timeline__wrapper" ref="wrapper"> <div class="timeline__container" ref="container"> <slot></slot> </div> </div> <div class="timeline__controls"> <button @click="scrollLeft">←</button> <button @click="scrollRight">→</button> </div> </div> </template> <script> export default { methods: { scrollLeft() { this.$refs.wrapper.scrollBy({ left: -200, behavior: 'smooth' }); }, scrollRight() { this.$refs.wrapper.scrollBy({ left: 200, behavior: 'smooth' }); } } } </script> <style scoped> .timeline { position: relative; padding: 1rem 0; overflow-x: auto; } .timeline__wrapper { display: flex; flex-direction: row; align-items: flex-start; } .timeline__container { display: flex; flex-direction: row; } .timeline__controls { position: absolute; top: 0; right: 0; display: flex; flex-direction: row; align-items: center; justify-content: space-between; width: 4rem; height: 100%; background-color: rgba(255, 255, 255, 0.8); z-index: 1; } .timeline__controls button { border: none; background-color: transparent; font-size: 1.2rem; cursor: pointer; outline: none; } </style> ``` 4. 在需要展示时间线的页面中使用 Timeline 和 TimelineItem 组件: ```vue <template> <div> <h1>My Timeline</h1> <Timeline> <TimelineItem date="2021-01-01"> <h3>Event A</h3> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> </TimelineItem> <TimelineItem date="2021-02-01"> <h3>Event B</h3> <p>Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> </TimelineItem> <TimelineItem date="2021-03-01"> <h3>Event C</h3> <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </TimelineItem> <TimelineItem date="2021-04-01"> <h3>Event D</h3> <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p> </TimelineItem> </Timeline> </div> </template> <script> import Timeline from './Timeline.vue'; import TimelineItem from './TimelineItem.vue'; export default { components: { Timeline, TimelineItem } } </script> ``` 通过以上步骤,你就可以创建一个简单的横向时间线组件,用户可以通过左右滚动查看不同时间点的事件。你可以根据自己的需求对组件进行进一步的定制和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值