当前位置:首页 > VUE

vue实现活动倒计时

2026-01-12 06:27:54VUE

实现活动倒计时的基本思路

在Vue中实现活动倒计时通常需要获取目标时间与当前时间的差值,通过定时器动态更新剩余时间。核心逻辑包括计算时间差、格式化和动态渲染。

使用计算属性与定时器

定义一个目标时间(如活动结束时间),通过setInterval每秒更新当前时间并计算剩余时间。将剩余时间分解为天、小时、分钟和秒。

<template>
  <div>
    <p>距离活动结束还剩: {{ days }}天 {{ hours }}小时 {{ minutes }}分钟 {{ seconds }}秒</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      targetTime: new Date('2023-12-31 23:59:59').getTime(),
      currentTime: new Date().getTime(),
      timer: null
    }
  },
  computed: {
    timeDiff() {
      return this.targetTime - this.currentTime;
    },
    days() {
      return Math.floor(this.timeDiff / (1000 * 60 * 60 * 24));
    },
    hours() {
      return Math.floor((this.timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    },
    minutes() {
      return Math.floor((this.timeDiff % (1000 * 60 * 60)) / (1000 * 60));
    },
    seconds() {
      return Math.floor((this.timeDiff % (1000 * 60)) / 1000);
    }
  },
  mounted() {
    this.timer = setInterval(() => {
      this.currentTime = new Date().getTime();
      if (this.timeDiff <= 0) {
        clearInterval(this.timer);
      }
    }, 1000);
  },
  beforeDestroy() {
    clearInterval(this.timer);
  }
}
</script>

使用第三方库优化

对于复杂场景(如跨时区、格式化),可以使用moment.jsday.js库简化日期操作。

import dayjs from 'dayjs';

export default {
  data() {
    return {
      targetTime: dayjs('2023-12-31 23:59:59'),
      now: dayjs(),
      timer: null
    }
  },
  computed: {
    countdown() {
      const diff = this.targetTime.diff(this.now, 'second');
      return {
        days: Math.floor(diff / 86400),
        hours: Math.floor((diff % 86400) / 3600),
        minutes: Math.floor((diff % 3600) / 60),
        seconds: diff % 60
      };
    }
  },
  mounted() {
    this.timer = setInterval(() => {
      this.now = dayjs();
      if (this.targetTime.diff(this.now, 'second') <= 0) {
        clearInterval(this.timer);
      }
    }, 1000);
  }
}

封装为可复用组件

将倒计时逻辑封装为组件,通过props接收目标时间,通过emit触发结束事件。

<template>
  <div>
    <slot :countdown="countdown">
      {{ countdown.days }}天 {{ countdown.hours }}小时 {{ countdown.minutes }}分钟 {{ countdown.seconds }}秒
    </slot>
  </div>
</template>

<script>
export default {
  props: {
    endTime: {
      type: [String, Date, Number],
      required: true
    }
  },
  data() {
    return {
      now: new Date().getTime()
    };
  },
  computed: {
    targetTime() {
      return new Date(this.endTime).getTime();
    },
    countdown() {
      const diff = this.targetTime - this.now;
      return {
        days: Math.floor(diff / (1000 * 60 * 60 * 24)),
        hours: Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
        minutes: Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)),
        seconds: Math.floor((diff % (1000 * 60)) / 1000)
      };
    }
  },
  mounted() {
    const timer = setInterval(() => {
      this.now = new Date().getTime();
      if (this.targetTime - this.now <= 0) {
        clearInterval(timer);
        this.$emit('end');
      }
    }, 1000);
    this.$once('hook:beforeDestroy', () => clearInterval(timer));
  }
};
</script>

注意事项

  • 性能优化:在组件销毁时清除定时器,避免内存泄漏。
  • 时区处理:确保目标时间与用户本地时区一致,必要时使用UTC时间。
  • 格式化:可通过padStart补零显示(如seconds.toString().padStart(2, '0'))。
  • 服务端渲染:在SSR场景下需兼容window对象缺失的情况。

vue实现活动倒计时

标签: 倒计时vue
分享给朋友:

相关文章

vue实现atm

vue实现atm

Vue实现ATM机模拟功能 使用Vue.js实现ATM机模拟功能需要结合组件化开发、状态管理以及用户交互逻辑。以下是关键实现步骤: 核心组件结构 创建以下Vue组件构建ATM界面: Car…

vue怎么实现选中删除

vue怎么实现选中删除

实现选中删除功能的方法 在Vue中实现选中删除功能通常涉及以下几个关键步骤: 数据绑定与选中状态管理 使用v-model或v-for指令绑定列表数据,结合复选框或单选按钮实现选中状态管理。例如…

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue实现文字播放栏

vue实现文字播放栏

Vue 实现文字播放栏(跑马灯效果) 方法一:使用 CSS 动画 + Vue 数据绑定 通过 CSS 的 @keyframes 实现动画效果,结合 Vue 的动态数据绑定控制内容。 <tem…

vue 实现简单登陆

vue 实现简单登陆

以下是一个使用 Vue 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-contain…

vue实现签约合作弹窗

vue实现签约合作弹窗

实现签约合作弹窗的步骤 使用Vue实现签约合作弹窗可以通过组件化方式完成,结合模态框和表单交互逻辑。以下是具体实现方法: 1. 创建弹窗组件 新建一个Vue组件文件(如SignContractDia…