Vue2时间轴(横向)

简介: 该横向时间轴组件 `HorizonTimeLine` 可自定义时间轴数据 (`timelineData`) 和初始选中年份 (`activeYear`)。通过点击不同的年份,可以改变当前激活的年份,并有相应的视觉反馈。该组件支持均匀自适应排列,且易于在其他页面中引入和使用。

可自定义设置以下属性:

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

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

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
  }
]
相关文章
|
9月前
Vue2 指定尺寸内进行拖拽
Vue2 指定尺寸内进行拖拽
119 0
|
JavaScript
vue可拖拽悬浮按钮组件
vue封装一个可拖拽,贴边吸附的悬浮按钮组件。
2076 0
|
4月前
|
JavaScript
|
6月前
|
前端开发 JavaScript 数据可视化
使用React的函数式组件实现一个具有过渡变化、刻度切换、点击高亮的柱状图DIY组件
本文展示了如何使用React的函数式组件实现一个具有过渡变化、刻度切换、点击高亮效果的自定义柱状图组件,并提供了完整的示例代码和实现效果。
84 1
|
6月前
|
JavaScript
Vue2横向文字滚动
这篇文章介绍了如何在Vue 2框架中实现一个横向滚动文本的组件,允许自定义滚动文本内容、滚动区域尺寸和滚动速度等属性。
183 0
|
6月前
|
存储 JavaScript 前端开发
vue前端自适应布局,一步到位所有自适应
【8月更文挑战第9天】在Vue前端实现全面自适应布局颇具挑战,但可通过多种方法达成接近目标的效果。首先,结合BootstrapVue或Element UI等响应式框架简化布局实现过程;其次,利用Sass或Less等预处理器增强CSS编写灵活性;再者,发挥Vue的响应式特性,动态调整组件尺寸与位置;同时采用Flexbox及媒体查询技术确保不同屏幕尺寸下的一致体验;针对移动设备,采取移动优先策略并使用专门框架优化表现;最后,多平台测试与细致调优保证布局效果。综合运用上述策略,可在复杂多变的设备环境中打造近乎完美的自适应布局。
320 1
|
7月前
|
JavaScript
vue 组件封装 -- 添加【呼吸】动画效果(两种: 淡入>>淡入,淡入>>淡出>>淡入)
vue 组件封装 -- 添加【呼吸】动画效果(两种: 淡入>>淡入,淡入>>淡出>>淡入)
91 0
|
7月前
|
JavaScript
vue 组件封装——可自由拖拽移动的盒子
vue 组件封装——可自由拖拽移动的盒子
113 0
|
9月前
|
前端开发 JavaScript
React中封装echarts图表组件以及自适应窗口变化
React中封装echarts图表组件以及自适应窗口变化
255 1
|
9月前
|
JavaScript 容器
vue echarts图表自适应屏幕变化
vue echarts图表自适应屏幕变化
373 0