当前位置:首页 > VUE

vue实现番茄钟

2026-02-19 16:02:20VUE

Vue实现番茄钟的步骤

创建Vue项目

使用Vue CLI创建一个新项目,确保已安装Node.js和Vue CLI。运行以下命令初始化项目:

vue create pomodoro-timer
cd pomodoro-timer

设计组件结构

src/components目录下创建PomodoroTimer.vue文件,作为番茄钟的核心组件。组件包含以下部分:

  • 计时器显示区域
  • 开始/暂停按钮
  • 重置按钮
  • 设置工作时间与休息时间

实现计时器逻辑

PomodoroTimer.vue中,使用data定义计时器状态:

vue实现番茄钟

data() {
  return {
    minutes: 25,
    seconds: 0,
    isWorking: true,
    isRunning: false,
    timer: null,
    workDuration: 25,
    breakDuration: 5
  }
}

添加方法控制计时器

实现开始、暂停和重置功能:

methods: {
  startTimer() {
    if (!this.isRunning) {
      this.isRunning = true;
      this.timer = setInterval(() => {
        if (this.seconds === 0) {
          if (this.minutes === 0) {
            this.switchMode();
          } else {
            this.minutes--;
            this.seconds = 59;
          }
        } else {
          this.seconds--;
        }
      }, 1000);
    }
  },
  pauseTimer() {
    clearInterval(this.timer);
    this.isRunning = false;
  },
  resetTimer() {
    clearInterval(this.timer);
    this.isRunning = false;
    this.minutes = this.isWorking ? this.workDuration : this.breakDuration;
    this.seconds = 0;
  },
  switchMode() {
    this.isWorking = !this.isWorking;
    this.minutes = this.isWorking ? this.workDuration : this.breakDuration;
    this.seconds = 0;
  }
}

添加模板与样式

template部分添加计时器界面:

vue实现番茄钟

<template>
  <div class="pomodoro-timer">
    <h3>{{ isWorking ? '工作时间' : '休息时间' }}</h3>
    <div class="timer-display">
      {{ String(minutes).padStart(2, '0') }}:{{ String(seconds).padStart(2, '0') }}
    </div>
    <div class="controls">
      <button @click="startTimer" :disabled="isRunning">开始</button>
      <button @click="pauseTimer" :disabled="!isRunning">暂停</button>
      <button @click="resetTimer">重置</button>
    </div>
    <div class="settings">
      <label>
        工作时间(分钟):
        <input type="number" v-model.number="workDuration" min="1">
      </label>
      <label>
        休息时间(分钟):
        <input type="number" v-model.number="breakDuration" min="1">
      </label>
    </div>
  </div>
</template>

添加样式

style部分添加基本样式:

<style scoped>
.pomodoro-timer {
  text-align: center;
  margin-top: 20px;
}
.timer-display {
  font-size: 3em;
  margin: 20px 0;
}
.controls button {
  margin: 0 10px;
  padding: 8px 16px;
  font-size: 1em;
}
.settings {
  margin-top: 20px;
}
.settings label {
  display: block;
  margin-bottom: 10px;
}
</style>

在主应用中使用组件

App.vue中导入并使用PomodoroTimer组件:

<template>
  <div id="app">
    <PomodoroTimer />
  </div>
</template>

<script>
import PomodoroTimer from './components/PomodoroTimer.vue'

export default {
  name: 'App',
  components: {
    PomodoroTimer
  }
}
</script>

运行项目

启动开发服务器查看效果:

npm run serve

功能扩展建议

  • 添加声音提示,在工作时间和休息时间切换时播放提示音
  • 实现任务列表功能,记录完成的工作周期
  • 添加响应式设计,优化移动端体验
  • 使用Vuex管理状态,实现更复杂的功能
  • 添加夜间模式或主题切换功能

标签: 番茄vue
分享给朋友:

相关文章

vue实现添加用户

vue实现添加用户

Vue 实现添加用户功能 数据绑定与表单设计 在 Vue 中实现添加用户功能,首先需要设计一个表单,用于收集用户输入的数据。通过 v-model 实现双向数据绑定,确保表单数据与 Vue 实例中的数据…

vue实现录播播放

vue实现录播播放

Vue 实现录播播放 使用 video.js 实现 安装 video.js 和相关的 Vue 适配器: npm install video.js @videojs-player/vue 在 Vue…

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双…

vue实现语音

vue实现语音

Vue 实现语音功能的方法 使用 Web Speech API Vue 可以结合 Web Speech API 实现语音识别和合成功能。Web Speech API 包含 SpeechRecognit…

vue实现rtc

vue实现rtc

Vue 实现 RTC 的步骤 安装依赖 在 Vue 项目中安装 WebRTC 相关依赖,如 simple-peer 或 peerjs,用于简化 WebRTC 的实现。 运行以下命令安装 simple-…

vue实现返回

vue实现返回

返回按钮功能实现 在Vue中实现返回功能通常有两种方式:使用浏览器历史记录的history.back()或结合Vue Router进行编程式导航。以下是具体实现方法: 使用浏览器API met…