vue活动倒计时

 一、封装组件countDown.vue

<template>
  <div
    :endTime="endTime"
    style="display: flex; justify-content: center; align-items: center"
  >
    <div class="blackBox">
      {{ tians }}
    </div>
    <div class="whiteFont">天</div>
    <div class="blackBox">{{ hours }}</div>
    <div class="whiteFont">时</div>
    <div class="blackBox">{{ minutes }}</div>
    <div class="whiteFont">分</div>
  </div>
</template>

<script>
export default {
  name: 'countDown',
  data () {
    return {
      tians: 0,
      hours: 0,
      minutes: 0,
      pageTimer: {}
    }
  },
  props: {
    endTime: {
      type: Number,
      default: 0
    }
  },
  mounted () {
    this.countdowm(this.endTime)
  },
  watch: {
    endTime (curVal, oldVal) {
      if (this.pageTimer) {
        // 清除所有的定时器,防止传过来的值变化出现闪烁的问题
        for (const each in this.pageTimer) {
          clearInterval(this.pageTimer[each])
        }
      }
      this.countdowm(curVal, oldVal)
    }
  },
  methods: {
    countdowm (timestamp, oldtime) {
      const self = this
      this.pageTimer.timer1 = setInterval(function () {
        const nowTime = new Date()
        const endTime = new Date(timestamp * 1000)
        const t = endTime.getTime() - nowTime.getTime()
        if (t > 0) {
          let tian = Math.floor(t / 86400000)
          let hour = Math.floor((t / 3600000) % 24)
          let min = Math.floor((t / 60000) % 60)
          tian = tian < 10 ? '0' + tian : tian
          hour = hour < 10 ? '0' + hour : hour
          min = min < 10 ? '0' + min : min
          self.tians = tian
          self.hours = hour
          self.minutes = min
        } else {
          clearInterval(self.pageTimer.timer1)
          self.tians = 0
          self.hours = 0
          self.minutes = 0
        }
      }, 1000)
    }
  }
}
</script>

<style lang="less" scoped>
.blackBox {
  width: 44px;
  height: 44px;
  background: #272931;
  border-radius: 12px;

  font-size: 24px;
  font-family: DIN Alternate-Bold, DIN Alternate;
  font-weight: bold;
  color: #ffffff;
  line-height: 44px;
  text-align: center;
}

.whiteFont {
  width: 24px;
  height: 44px;
  font-size: 24px;
  font-family: PingFang SC-Medium, PingFang SC;
  font-weight: 500;
  color: #ffffff;
  line-height: 44px;
  text-align: center;
  margin: 0 8px;
}
</style>

二、引用组件

<count-down :endTime="1692460800" />

import countDown from './module/countDown.vue'

  components: {
    countDown
  }

三、时间戳转换

在线时间戳转换工具

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值