一个vue时间轴功能实现

在这里插入图片描述

<template>
  <div ref="timecontainer" @mouseleave="mouseleave" class="history">
    <div class="left-shadow"></div>
    <div
      @mousedown="mousedown"
      @mouseup="mouseup"
      :style="objStyle"
      ref="target"
      class="time-axis-outer"
    >
      <div class="time-axis">
        <div v-for="(item, index) in list" :key="index">
          <div class="year-title">{{ item.text }}</div>
          <div class="year">{{ item.year }}</div>
        </div>
      </div>
      <div class="bgline">
        <div
          class="bg-interval"
          v-for="(item, index) in list"
          :key="index"
        ></div>
      </div>
    </div>
    <div class="right-shadow"></div>
  </div>
</template>
<script lang="ts">
import { Component, Vue, Emit, Prop, Watch } from "vue-property-decorator";

@Component({
  components: {},
})
export default class App extends Vue {
  objStyle = {
    left: 0 + "px",
  };
  ele = null as HTMLDivElement;
  viewArea = null as HTMLDivElement;

  list = [
    { year: "1982", text: "aaaa" },
    { year: "1995", text: "bbbb" },
    { year: "2004", text: "cccc" },
    { year: "2009", text: "dddd" },
    { year: "2010", text: "eeee" },
    { year: "2012", text: "ffff" },
    { year: "2018", text: "gggg" },
    { year: "2018", text: "hhhhh" },
    { year: "2019", text: "iiiii" },
    { year: "2020", text: "jjjj" },
    { year: "2021", text: "kkkk" },
  ];
  isdown = false;
  tempfn = null as any;
  callbackarr: (() => void)[] = [];

  movecallback = (clientx: number, initleft: number, start: number) => {
    let that = this;

    return function (e: MouseEvent) {
      let offset = e.clientX - clientx; //实时的减去点击时候的位置

      const left = initleft + offset;

      if (left > -start && left <= 0) {
        that.objStyle.left = left + "px";
      }
    };
  };
  mouseupcallback = (e: MouseEvent) => {
    this.mouseup();
  };

  mounted() {
    this.ele = this.$refs.target as HTMLDivElement;
    this.viewArea = this.$refs.timecontainer as HTMLDivElement;
    this.objStyle.left = this.ele.offsetLeft + "px";

    document.body.addEventListener("mouseup", this.mouseupcallback);
  }

  mousedown(e: MouseEvent) {
    this.isdown = true;

    let start = this.ele.offsetWidth - this.viewArea.offsetWidth;

    this.tempfn = this.movecallback(e.clientX, this.ele.offsetLeft, start);

    this.callbackarr.push(this.tempfn);

    (this.$refs.timecontainer as HTMLDivElement).addEventListener(
      "mousemove",
      this.tempfn
    );
  }

  mouseleave() {
    this.mouseup();
  }

  mouseup() {
    this.callbackarr.forEach((item) => {
      (this.$refs.timecontainer as HTMLDivElement).removeEventListener(
        "mousemove",
        item
      );
    });

    this.tempfn = null;
    this.isdown = false;
  }

  beforeDestroy() {
    document.body.removeEventListener("mouseup", this.mouseupcallback);
  }
}
</script>
<style scoped lang="scss">
.history {
  position: relative;
  height: 320px;
  overflow: hidden;
  .time-axis-outer {
    position: absolute;
    width: var(--contentWidth);
    height: 300px;
    display: flex;
    flex-direction: column;
    align-items: center;
    .bgline {
      display: flex;
      justify-content: space-around;
      align-items: end;
      left: 0;
      top: 170px;
      width: var(--contentWidth);
      height: 10px;
      transform: translateY(-120px);
      background: RGBA(164, 196, 253, 1);
      .bg-interval {
        width: 10px;
        height: 30px;
        background: RGBA(164, 196, 253, 1);
      }
    }
    .time-axis {
      left: 0;
      top: 0;
      height: 290px;
      width: var(--contentWidth);
      display: flex;
      justify-content: space-around;
      cursor: pointer;
      & > div {
        height: 290px;
        width: 410px;

        .year-title {
          margin-top: 60px;
        }
        .year {
          margin-top: 120px;
          font-size: 30px;
          font-family: PingFang SC-Bold, PingFang SC;
          font-weight: bold;
          color: #a4c4fd;
          height: 35px;
          line-height: 35px;
        }
      }
    }
  }
  .left-shadow {
    z-index: 1;
    position: absolute;
    width: 300px;
    height: 320px;
    left: 0;
    top: 0;
    background: linear-gradient(90deg, #030e26 0%, rgba(3, 14, 38, 0) 100%);
  }

  .right-shadow {
    z-index: 1;
    position: absolute;
    right: 0;
    top: 0;
    width: 300px;
    height: 320px;
    transform: rotate(180deg);
    background: linear-gradient(90deg, #030e26 0%, rgba(3, 14, 38, 0) 100%);
  }
}
</style>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue实现时间轴功能可以通过以下步骤: 1. 定义数据结构:时间轴需要一个数据结构来存储每个时间点的信息,可以定义一个数组,每个元素包含时间、标题、描述等信息。 2. 创建组件:创建一个组件来展示时间轴,可以使用v-for指令遍历数据结构,将每个时间点渲染为一个卡片。 3. 样式设计:设计时间轴的样式,可以使用CSS实现,包括卡片的布局、字体颜色、背景色等。 4. 添加交互:为了让用户可以通过点击时间点来查看详细信息,可以在组件中添加点击事件,通过传递参数来显示对应的卡片。 下面是一个简单的时间轴组件实现: ``` <template> <div class="timeline"> <div class="timeline-item" v-for="(item, index) in items" :key="index" @click="showDetail(item)"> <div class="timeline-time">{{ item.time }}</div> <div class="timeline-content"> <h3>{{ item.title }}</h3> <p>{{ item.description }}</p> </div> </div> </div> </template> <script> export default { data() { return { items: [ { time: "2021-01-01", title: "事件1", description: "事件1描述" }, { time: "2021-02-01", title: "事件2", description: "事件2描述" }, { time: "2021-03-01", title: "事件3", description: "事件3描述" }, { time: "2021-04-01", title: "事件4", description: "事件4描述" }, ], }; }, methods: { showDetail(item) { // 弹出详细信息 console.log(item); }, }, }; </script> <style> .timeline { border-left: 2px solid #ccc; padding: 10px; } .timeline-item { display: flex; margin-bottom: 20px; cursor: pointer; } .timeline-time { font-size: 16px; font-weight: bold; color: #333; width: 100px; } .timeline-content { margin-left: 20px; } .timeline-content h3 { font-size: 18px; font-weight: bold; color: #333; margin-bottom: 10px; } .timeline-content p { font-size: 14px; color: #666; } </style> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值