Vue2时间轴(横向)

本文介绍了一个用于创建横向时间轴的Vue组件HorizonTimeLine.vue,该组件支持自定义设置时间轴数据和初始选中年份。组件采用空间均匀分布,通过点击可切换选中年份,并具有高亮和悬停效果。在实际使用中,可通过引入组件并传入时间轴数据和初始年份来展示时间轴。示例展示了如何配置和使用该组件。
摘要由CSDN通过智能技术生成

可自定义设置以下属性:

  • 时间轴数据(timelineData),必传
  • 初始选中年份(activeYear),默认2020

效果如下图(自适应均匀排列,每个元素周围分配相同的空间<space-around>):

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAdGhlTXVzZUNhdGNoZXI=,size_20,color_FFFFFF,t_70,g_se,x_16

①创建横向时间轴组件HorizonTimeLine.vue:

<template>
  <div class="m-timeline-wrap">
    <div class="m-time-dot">
      <div
        :class="['m-dot-box', {'active': active===item.year}]"
        @click="onClickYear(item.year)"
        v-for="(item, index) in timelineData"
        :key="index">
        <p class="u-year">{{ item.year }}</p>
        <div class="m-dot">
          <div class="u-dot"></div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'HorizonTimeLine',
  props: {
    timelineData: { // 时间轴数据
      type: Array,
      required: true,
      default: () => {
        return []
      }
    },
    activeYear: { // 初始选中年份
      type: Number,
      default: 2020
    }
  },
  data () {
    return {
      active: this.activeYear
    }
  },
  methods: {
    onClickYear (year) {
      if (this.active !== year) {
        this.active = year
        console.log('hello', year)
      }
    }
  }
}
</script>
<style lang="less" scoped>
@themeColor: #1890FF;
.m-timeline-wrap {
  margin: 150px auto;
  height: 3px;
  background: #8dc6f5;
  .m-time-dot {
    display: flex;
    justify-content: space-around;
    .m-dot-box {
      cursor: pointer;
      text-align: center;
      transform: translateY(-100%+14px);
      .u-year {
        font-size: 28px;
        font-weight: 500;
        color: #333;
        transform: translateY(-8px);
        transition: all 0.3s ease;
      }
      .m-dot {
        margin: 0 auto;
        width: 14px;
        height: 14px;
        background: #8dc6f5;
        border-radius: 50%;
        transition: all 0.3s ease;
        .u-dot {
          width: 14px;
          height: 14px;
          background: #8dc6f5;
          border-radius: 50%;
          transition: all 0.3s ease;
        }
      }
    }
    .m-dot-box:hover {
      .u-year {
        color: @themeColor;
      }
      .m-dot {
        .u-dot {
          background: @themeColor;
        }
      }
    }
    .active {
      .u-year {
        transform: scale(2) translateY(-18px); // 同时设置多个transform属性只需用空格间隔,执行时从后往前执行!
        color: @themeColor;
      }
      .m-dot {
        transform: scale(3);
        .u-dot {
          transform: scale(0.67);
          background: @themeColor;
        }
      }
    }
  }
}
</style>

②在要使用的页面引入:

<HorizonTimeLine :timelineData="timelineData" :activeYear="2021" />
import HorizonTimeLine from '@/components/HorizonTimeLine'
components: {
  HorizonTimeLine
}
timelineData: [
  {
    year: 2022
  },
  {
    year: 2021
  },
  {
    year: 2020
  },
  {
    year: 2019
  },
  {
    year: 2018
  }
]
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值