时间轴线—自动轮播:配合展示数据

一、先上一波效果图

二、实现思路 

  1. 调整好布局:主要是X轴,以及时间圆点。
  2. 时间数据: 使用div标签,使其浮动,给奇或偶数添加 margin-top,上下分开。
    <div class="year">
              <div
                @click="onChange(index)"
                v-for="(item, index) in list"
                :key="index"
                :class="{ ever: index % 2 === 0 }"
                :style="`width: ${width}%;`"
              >
                {{ item[0].year }}
              </div>
    </div>
    
    
    
    .year {
            display: flex;
            justify-content: space-around;
            div {
              color: #c0dcf1;
              position: relative;
              margin-top: 10px;
              cursor: pointer;
            }
            .ever {
              top: 35px;
            }
          }

  3. 圆点随时间跳动:给其设置一个动态的左间距
     

     <div class="circle" :style="`left: calc(${(widthLeft / list.length) * 95 + 4}%)`">
        </div>

三、代码实现 

<template>
  <div class="center" @mouseenter="handleMouseEnter" @mouseleave="handleMouseLeave">
    <div class="LTimeLine">
      <div class="yearRange">
        <div class="year">
          <div @click="onChange(index)" v-for="(item, index) in list" :key="index"
            :class="{ ever: index % 2 === 0 }" :style="`width: ${width}%;`">
            {{ item[0].year }}
          </div>
        </div>
      </div>
      <div class="lineImg"></div>
    </div>
    <div class="axios"></div>
    <div class="circle" :style="`left: calc(${(widthLeft / list.length) * 95 + 4}%)`">
    </div>
    <div class="popoverSelf">
      <div class="PTitle">{{ year }}年生态文明创建大事记</div>
      <div class="PContent">
        获得荣誉:<span>{{ count }}项</span>
      </div>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  name: "center",
  components: {},
  data() {
    return {
      width: 0,
      widthLeft: 0,
      time: 0,
      itemData: [],
      list: [],
    };
  },

  computed: {
    year() {
      return this.list[this.widthLeft][0].year;
    },
    count() {
      return this.list[this.widthLeft][0].count;
    },
  },
  created() {
    this.getYearList();
  },
  methods: {
    initData() {
      this.widthLeft++;
      if (this.widthLeft === this.list.length) {
        this.widthLeft = 0;
      }
      this.$emit("getYear", this.year);
    },

    getYearList() {
      request({
        url: "xxxxxxxxxxxxxxxxxxxxxxxxxx",
        method: "get",
      }).then((res) => {
        if (res.code === 200 && res.data) {
          this.list = Object.values(res.data);
          this.width = 100 / this.list.length;
          this.$emit("getYear", this.year);
        }
      });
    },

    //切换年份
    onChange(val) {
      this.widthLeft = val;
      this.$emit("onChangeYear", this.year);
    },

    handleMouseEnter() {
      this.$emit("mouseLeave");
    },
    handleMouseLeave() {
      this.$emit("mouseOut");
    },
  },
};
</script>

<style lang="scss" scoped>
.center {
  height: auto;
  width: 750px;
  .LTimeLine {
    align-self: center;
    width: 100%;
    height: 100px;
    display: flex;
    align-items: center;
    position: relative;
    justify-content: center;
    .yearRange {
      width: 100%;
      height: 10px;
      position: absolute;
      display: flex;
      justify-content: space-around;
      letter-spacing: 1px;
      .year {
        display: flex;
        justify-content: space-around;
        div {
          color: #c0dcf1;
          position: relative;
          margin-top: 10px;
          cursor: pointer;
        }
        .ever {
          top: 35px;
        }
      }
    }
    .lineImg {
      width: 100%;
      height: 150px;
      background: url('../assets/center/bottom.png') no-repeat;
      background-size: 100% 100%;
    }
  }
}
.axios {
  width: 800px;
  height: 250px;
  position: absolute;
  top: -42px;
  background: url('../assets/center/axis2.png') no-repeat;
  background-size: 95% 100%;
  pointer-events: none;
}
.circle {
  position: absolute;
  top: 74px;
  left: 10%;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background-color: #6dcbef;
}
.popoverSelf {
  position: absolute;
  bottom: 80px;
  left: 180px;
  // top: 0;
  // transform: translateY(-120%);
  width: 380px;
  height: 147px;
  background: url('../assets/center/infoBlock.png') no-repeat;
  background-size: 100% 100%;
  display: flex;
  flex-direction: column;
  padding: 4px 10px;
  align-items: center;
  padding-top: 20px;

  .PTitle {
    align-items: start;
    font-size: 18px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #d5f0ff !important;
    width: 100%;
    text-align: center;
  }
  .PContent {
    margin: auto;
    font-size: 14px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #d5f0ff !important;
    line-height: 24px;
    span {
      font-size: 20px;
    }
  }
  .typeItem {
    cursor: pointer;
  }
}
</style>

四、配合父组件实现轮播 

  // 字组件 鼠标进入 关闭定时器
    handleMouseEnter() {
      clearInterval(this.time);
    },

    //  字组件 鼠标离开 开启定时器
    handleMouseLeave() {
      this.time = setInterval(() => {
        this.$refs.centerCom.initData();
        this.$refs.leftThree.initData(this.year);
      }, 5000);
    },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要实现ECharts折线图的数据动轮,你可以使用ECharts的动态数据更新功能。以下是一种实现方式: 1. 首先,创建一个包含所有需要轮数据的数组。例如,假设你有一个名为`data`的数组,其中包含多个时间点的数据。 2. 在ECharts的配置项中,设置一个初始的数据项,用于展示第一个时间点的数据。 3. 使用`setInterval`函数来定时更新数据。在每个时间间隔内,通过修改ECharts配置项中的数据项,更新到下一个时间点的数据。 下面是一个示例代码: ```javascript // 假设data为包含多个时间点的数据的数组 var data = [ { time: '2021-01-01', value: 100 }, { time: '2021-01-02', value: 200 }, // ... ]; // 初始化ECharts实例 var myChart = echarts.init(document.getElementById('chart')); // 设置初始的数据项 myChart.setOption({ xAxis: { type: 'category', data: [data[0].time] }, series: [{ type: 'line', data: [data[0].value] }] }); // 定义计数器和时间间隔 var currentIndex = 0; var interval = 2000; // 每2秒更新一次数据 // 定时更新数据 setInterval(function() { // 更新数据索引 currentIndex = (currentIndex + 1) % data.length; // 更新ECharts配置项中的数据项 myChart.setOption({ xAxis: { data: [data[currentIndex].time] }, series: [{ data: [data[currentIndex].value] }] }); }, interval); ``` 在上述代码中,我们设置了一个定时器,每2秒更新一次数据。通过更新ECharts配置项中的`xAxis.data`和`series.data`,我们可以实现折线图的数据动轮效果。 你可以根据实际需求修改时间间隔、数据格式等参数,以适应你的项目要求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冬问春风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值