倒计时(所有地区显示剩余时间一样)

<template>
  <div class="countDown">
    <div v-if="!isCountDownOver">
      <span class="title">{{ LANG.test1.desc100  }}</span>
      <span class="time">{{ countDownHour }}</span>
      <span class="time">{{ countDownMinutes }}</span>
      <span class="time">{{ countDownSeconds }}</span>
    </div>
    <div v-else class="timeOver"></div>
  </div>
</template>

<style lang="less" scoped>
.countDown {
  width: 100%;
  font-size: .3rem;
  margin: .1rem auto;

}
.countDown .title {
  font-weight: 700;
  color: #ffffff;

  margin-right: .05rem;
  //
}
.countDown .time {
  background-color: #2d67a3;
  color: #fff;
  margin: 0 .02rem;
  padding: 0 .05rem;
  border-radius: .03rem;
}
.timeOver {
  text-align: center;
  font-weight: 700;
  color: #ffffff;
}
</style>

<script>
export default {
  name: "countDown",
  data() {
    return {
      isCountDownOver: false,
      // countDownD: "00",
      countDownHour: "00",
      countDownMinutes: "00",
      countDownSeconds: "00",
    };
  },
  mounted() {
    this.timer();
  },
  methods: {
    //  时间格式化
    timeFormat(param) {
      return param < 10 ? "0" + param : param;
    },
    //  倒计时功能
    timer() {
      let dateNow  = new Date();
      let nowTime = Date.UTC(dateNow.getUTCFullYear(),
                            dateNow.getUTCMonth(),
                            dateNow.getUTCDate(),
                            dateNow.getUTCHours(),
                            dateNow.getUTCMinutes(),
                            dateNow.getUTCSeconds());
      let failTime = Date.UTC(2022,0,25,12,0,0);
      let leftTime = failTime - nowTime;

      let interval = setInterval(() => {
        let timeObj = {};
        leftTime = leftTime - 1000;
        if (leftTime > 0) {
          let d = parseInt(leftTime / 1000 / 60 / 60 / 24);
          let hour = parseInt(((leftTime / 1000 / 60 / 60) % 24) + 24 * d);
          let minutes = parseInt((leftTime / 1000 / 60) % 60);
          let seconds = parseInt((leftTime / 1000) % 60);
          timeObj = {
            hour: this.timeFormat(hour),
            minutes: this.timeFormat(minutes),
            seconds: this.timeFormat(seconds)
          };
        } else {
          this.isCountDownOver = true;
          clearInterval(interval); // 清除定时器
        }
        this.countDownHour = timeObj.hour;
        this.countDownMinutes = timeObj.minutes;
        this.countDownSeconds = timeObj.seconds;
      }, 1000);
    },
  },
};
</script>
posted @   koo-  阅读(52)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示