封装vue倒计时组件,方便大家参考。

组件代码封装,这里面startTime格式是YYYY-MM-DD, endTime格式是YYYY-MM-DD
<template>
  <div class="countDownClass">
    <span>{{countDownText}}</span>
    <span>
      <span>{{hhssmm.hou}}</span>
      <span>:</span>
      <span>{{hhssmm.min}}</span>
      <span>:</span>
      <span>{{hhssmm.sec}}</span>
    </span>

  </div>
</template>

<script>

  import moment from 'moment'

  export default {
    name: "CountDown",
    props: {
      startTime: {
        type: String,
        required: true,
      },
      endTime: {
        type: String,
        required: true,
      }
    },
    data() {
      return {
        countDownText: '',
        hhssmm:{},
      }
    },
    computed: {
    },
    async created() {
      this.updateState();
      this.timeInterval=setInterval(()=>{
        this.updateState()
      },1000)
    },
    updated(){
      if(this.end||this.done){
        clearInterval(this.timeInterval)
      }
    },
    methods: {

      /**
       * 时间小于10补零
       * */
      timeFormat(param) {
        return param < 10 ? "0" + param : param;
      },

      /**
       * 时间格式化HHmmss
       * */
      formateHHmmss(time){
        let day = parseInt(time /1000 / (60 * 60 * 24));
        let hou = parseInt((time  /1000 % (60 * 60 * 24)) / 3600) + day * 24;
        let min = parseInt(((time /1000  % (60 * 60 * 24)) % 3600) / 60);
        let sec = parseInt(((time /1000 % (60 * 60 * 24)) % 3600) % 60);
        let HHmmss = {
          hou: this.timeFormat(hou),
          min: this.timeFormat(min),
          sec: this.timeFormat(sec)
        };
        return HHmmss;
      },

      /**
       * 更新倒计时状态
       */
      updateState() {
        console.log(this.startTime);

        let starTime = new Date(moment(this.startTime).format("YYYY/MM/DD HH:mm:ss")).getTime();
        console.log(moment(this.startTime).format("YYYY/MM/DD HH:mm:ss"))
        let endTime = new Date(moment(this.endTime).format("YYYY/MM/DD HH:mm:ss")).getTime();
        var test = new Date(endTime);
        //console.log(test.getFullYear() + "-" + this.timeFormat(test.getMonth()) + "-" + this.timeFormat(test.getDate()) + " " + this.timeFormat(test.getHours()) + ":" + this.timeFormat(test.getMinutes()) + ":" + this.timeFormat(test.getSeconds()))
        let nowTime = new Date().getTime();
        let beginTime = starTime - nowTime;
        let finishTime = endTime - nowTime;
        let hhmmss = null;
        if (beginTime >= 0) {
          let hou = this.formateHHmmss(beginTime).hou;
          let min = this.formateHHmmss(beginTime).min;
          let sec = this.formateHHmmss(beginTime).sec;
          hhmmss = {
            hou: hou,
            min: min,
            sec: sec
          };
          this.hhssmm = hhmmss;
          this.start=true;
          this.countDownText="秒杀已开始";
        } else if (finishTime >= 0) {
          let hou = this.formateHHmmss(finishTime).hou;
          let min = this.formateHHmmss(finishTime).min;
          let sec = this.formateHHmmss(finishTime).sec;
          hhmmss = {
            hou: hou,
            min: min,
            sec: sec
          };
          this.hhssmm = hhmmss;
          this.countDownText=`距离活动结束`;
        } else {
          hhmmss = {
            hou: "00",
            min: "00",
            sec: "00"
          };
          this.end=true;
          this.countDownText="活动已结束";
        }
      }
    },
    beforeDestroy() {
      clearInterval(this.timeInterval)
    }
  }
</script>

<style scoped>
  /* 秒杀 */
  .countDownClass {
    display: flex;
    align-items: center;
    flex-direction: row;
    margin: 0 0 0.1rem 0;
    color: #fd286f;
  }
  .countDownClass > span:last-child {
    margin-left: 0.1rem;
  }
  .countDownClass > span:last-child span:nth-child(odd) {
    color: #fff;
    font-weight: bold;
    display: inline-block;
    border-radius: 0.1rem;
    background-color: #fd286f;
    padding: 0.05rem 0.05rem 0.05rem 0.05rem;
  }
</style>

 

组件引入方式

<CountDown :startTime="YYYY-MM-DD" :endTime="YYYY-MM-DD"></CountDown>

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值