当前位置:首页 > VUE

vue实现活动倒计时

2026-01-12 06:27:54VUE

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

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

vue实现活动倒计时

使用计算属性与定时器

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

vue实现活动倒计时

<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实现搜索查询

实现搜索查询的基本思路 在Vue中实现搜索查询功能通常涉及以下几个核心部分:数据绑定、事件监听、过滤逻辑和UI反馈。以下是一个完整的实现方案。 数据绑定与输入框 创建一个Vue组件,包含一个输入框用…

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tabl…

vue插槽实现

vue插槽实现

插槽的基本概念 Vue插槽(Slot)是一种内容分发机制,允许父组件向子组件传递模板片段,子组件通过<slot>标签定义接收位置。插槽的核心作用是增强组件的灵活性和复用性。 默认插槽 子…

vue实现素材

vue实现素材

Vue 实现素材的方法 使用 Vue 组件管理素材 在 Vue 项目中,可以通过组件化的方式管理素材。创建一个专门的组件来加载和显示素材,例如图片、视频或音频文件。组件可以接收素材的路径或 URL 作…

vue实现购票

vue实现购票

Vue 实现购票功能 数据结构设计 购票功能通常需要以下数据结构: 场次信息(时间、地点、价格) 座位信息(可选座位、已售座位) 用户订单信息 示例数据结构: data() { return…

vue实现mouseout

vue实现mouseout

Vue 实现 mouseout 事件的方法 在 Vue 中实现 mouseout 事件可以通过多种方式完成,以下是几种常见的方法: 使用 v-on 指令 Vue 提供了 v-on 指令(或简写为 @…