首页 > 其他分享 >vue8小时带刻度的时间轴,根据当前时间实时定位

vue8小时带刻度的时间轴,根据当前时间实时定位

时间:2023-05-16 16:34:08浏览次数:34  
标签:刻度 current second 时间轴 time position absolute vue8 255

效果图:

 需求:

1 开始时间、结束时间可配置
2 时差固定8小时
3 根据当前时间初始化位置
4 每隔5s刷新位置
5 超过结束时间停止刷新

HTML:

  <div class="time-axis">
    <div class="startTime">{{start_time}}</div>
    <div class="endTime">{{end_time}}</div>
    <!-- 小时刻度 -->
    <div class="hourLine">
      <div class="line" v-for="index of 8" :key="index" :style="{left: `${90*(index-1)}px`}">
        <div class="secondLine">
          <div class="second" v-for="index of 4" :key="index" :style="{left: `${18*(index-1)}px`}"></div>
        </div>
      </div>
    </div>
    <!-- 指针 -->
    <div class="point" :style="{left: `${current_position}px`}" v-if="pointFlag">
      <div class="currentTime">{{current_time}}</div>
      <img src="@/assets/img/gateway/timeLine_point.png" alt="">
    </div>
  </div>

JS:

props: {
    start_time: {
      type: String,
      default: '',
    },
    end_time: {
      type: String,
      default: '',
    },
    currentTimeFromP: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      current_time: '10:00:00',
      allSecond: 0,
      st_second: '',
      et_second: '',
      current_second: '',
      current_position: '',
      timer: null,
      pointFlag: true,
    }
  },
  beforeDestroy() {
    if(this.timer) {
      clearInterval(this.timer); 
    }
  },
  mounted() {
    this.st_second = this.hmsToS(this.start_time)
    this.et_second = this.hmsToS(this.end_time)
    // 8小时总秒
    this.allSecond = this.hmsToS(this.end_time) - this.hmsToS(this.start_time)
    // 计算当前位置
    this.current_position = this.bibliography() * 722
    // 判断当前时间是否超过结束时间或者小于开始时间
    if(this.current_second>=this.et_second || this.current_second<this.st_second ) {
      this.$message.warning('当前时间不在服务范围内')
      this.pointFlag = false
    } else {
      this.timer = setInterval(() => {
        if(this.current_second>=this.et_second || this.current_second<this.st_second ) {
          clearInterval(this.timer)
          this.$message.warning('当前时间不在服务范围内')
          this.pointFlag = false
        }
        this.current_position = this.bibliography() * 722
      }, 5000)
    }
  },
  methods: {
    // 比例 = (当前时间 - 开始时间) / 总秒数
    bibliography() {
      // 当前时间秒数
      this.current_second = this.hmsToS(this.nowDataToHMS())
      let key = (this.current_second - this.st_second) / this.allSecond
      return key
    },
    // 时分秒转化秒
    hmsToS(e) {
      var time = e;
      var len= time.split(':')
      if(len.length==3){
        var hour = time.split(':')[0];
        var min = time.split(':')[1];
        var sec = time.split(':')[2];
        return  Number(hour*3600) + Number(min*60) + Number(sec);
      }
      if(len.length==2){
        var min = time.split(':')[0];
        var sec = time.split(':')[1];
        return Number(min*60) + Number(sec);
      }
      if(len.length==1){
        var sec = time.split(':')[0];
        return Number(sec);
      }
    },
    // 当前时间时分秒
    nowDataToHMS() {
      let myDate = new Date()
      let str = myDate.toTimeString()
      this.current_time = str.substring(0,8)
      return this.current_time
    },
  },

CSS:

.time-axis {
  position: relative;
  height: 26px;
  border-left: 2px solid rgba(255,255,255,.6);
  border-right: 2px solid rgba(255,255,255,.6);
  width: 720px;
  background: rgba(0,0,0,.2);
  color: #fff;
  .startTime {
    position: absolute;
    top: -20px;
    left: -25px;
    color: #fff;
  }
  .endTime {
    position: absolute;
    top: -20px;
    right: -25px;
    color: #fff;
  }
  .hourLine {
    height: 70%;
    width: 100%;
    position: absolute;
    bottom: 0;
    display: flex;
    .line {
      position: absolute;
      width: 90px;
      height: 100%;
      border-right: 1px solid rgba(255,255,255,.6);
      &:nth-child(8) {
        border-right: none;
      }
      .secondLine {
        height: 50%;
        width: 100%;
        position: absolute;
        bottom: 0;
        display: flex;
        .second{
          position: absolute;
          width: 18px;
          height: 100%;
          border-right: 1px solid rgba(255,255,255,.3);
        }
      }
    }
  }
  .point {
    position: absolute;
    left: -8px;
    .currentTime {
      position: absolute;
      bottom: -10px;
      left: -18px;
      color: #00D4FF;
    }
    img {
      width: 16px;
      height: 46px;
    }
  }
}

***如有转载请注明出处!!!

标签:刻度,current,second,时间轴,time,position,absolute,vue8,255
From: https://www.cnblogs.com/Lu-Lu/p/17406062.html

相关文章

  • opencv 表识别 工业表智能识别 数字式表盘识别,指针式表盘刻度识别,分为表检测,表盘纠正,
    opencv表识别工业表智能识别数字式表盘识别,指针式表盘刻度识别,分为表检测,表盘纠正,刻度分割,刻度拉直识别YYID:37400640060136589......
  • 解决Echarts折线图的y轴刻度值与图表渲染不一致的BUG
    目录问题描述:原因分析:解决方案: 最终效果:问题描述:在做Echarts图表时,发现当有多条折线图渲染时,折线图和y轴上面的数字不匹配的bug。如下图所示,本文记录一下解决方案。原因分析:怀疑过因为数据格式不符合标准导致的,通过修改格式发现并没有解决问题。后怀疑是不是配置的原因,经过上网查......
  • cocos-js 播放cocos studio创建的时间轴场景动画
    ctor:1、加载场景动画json2、把场景动画的node添加到层3、找到运动的node4、关联动画和nodeload.node.runAction(load.action)播放:this._load.action.gotoFrameAndPlay(0,100,false);如果需要改变运动的node下面的子节点,可以把子节点removeFromParent然后创建一个新的Sp......
  • QWT--自定义坐标轴刻度值
    一、效果展示定义xBottom为带单位的刻度值  定义xBottom为分钟:秒数的刻度值  二、具体步骤2.1、自定义坐标轴自定义坐标轴刻度,需要继承自QwtScaleDraw,然后重写如下虚函数:virtualQwtTextlabel(doublev)const;定义一个规则,然后设置坐标轴范围时,按定义的规则......
  • echarts 双y轴0刻度线对称
    第一步:分别找出双y轴的最大最小值constmax1=Math.max(...data1);constmin1=Math.min(...data1);constmax2=Math.max(...data2);constmin2=Math.min(...data2);第二步:计算两组数据范围的比值(相当于比例尺)constratio=(max1-min1)/(max2-min2);第三步:......
  • 一口气学完Hudi——核心概念之时间轴
    ApacheHudi是一个基于Hadoop的分布式数据存储系统,支持存储结构化和非结构化数据。Hudi的时间轴(TimeLine)是其重要的组成部分,用于管理和跟踪数据的变化历史。在本文中,我将详细介绍Hudi时间轴的基本概念、特点以及如何使用它来进行数据管理。一、Hudi时间轴的基本概念H......
  • Codeforces Gym 103931F - Forest of Magic(时间轴分块+线段树合并)
    一个巨烦的时间轴分块做法,有点类似于P2137Gty的妹子树先考虑静态的情况。看上去就一脸线段树合并对吧?一次修改的操作对一个点\(x\)贡献可以写成\(k·dep_x+b\)的形式,开两棵线段树合并维护一次项和零次项系数即可。由于静态问题可做,因此考虑时间轴分块。设阈值\(B\),每\(B......
  • 轴刻度为字符串
    上面的程序运行起来,X轴的刻度是数字,如果我们希望轴刻度是文字怎么做呢?我们参考了这个网址的介绍: https://stackoverflow.com/questions/31775468/show-string-values-......
  • ggplot2中设置标签刻度的粗细和长度
     001、使用绘制散点图进行测试。a、直接绘制散点图x<-1:10y<-seq(1,1000,100)dat<-data.frame(x,y)##生成测试数据ggplot(dat,aes......
  • echarts折线图y轴刻度根据数据自适应
      参考:https://blog.csdn.net/wozhenhaokan/article/details/107245112......