vue 实现 tomato timer(蕃茄钟)实例讲解

yizhihongxing

下面是详细讲解 “vue 实现 tomato timer(蕃茄钟)实例讲解”的完整攻略。

一、项目介绍

蕃茄钟是一种时间管理方法,采用对数目进行计数的方式来提高工作效率。本项目中我们将使用 Vue 来实现一个基础版的蕃茄钟。

二、项目结构

├── App.vue
├── main.js
├── components
│   ├── Timer.vue
│   └── Tomato.vue
├── assets
│   └── tomato.png
└── styles
    └── index.css
  • App.vue: 根组件,用来包容计时器组件和蕃茄时钟组件
  • components/Timer.vue: 计时器组件,显示倒计时和计时状态特效
  • components/Tomato.vue: 蕃茄钟组件,用来设置每个蕃茄的时间和完成数量
  • assets/tomato.png: 蕃茄钟图标
  • styles/index.css: 样式文件

三、项目实现

1、Tomato.vue 组件

蕃茄钟组件中主要包含一个表单用来输入每个蕃茄的时间,还有一个用来显示当前完成的蕃茄数量。

<template>
  <div class="tomato">
    <div class="tomato-header">
      <img src="../../assets/tomato.png" alt="Tomato" width="80" height="80">
      <h2 class="tomato-title">Tomato Timer</h2>
    </div>
    <div class="tomato-body">
      <form class="tomato-form" @submit.prevent="submit">
        <label class="tomato-label" for="time">设置每个蕃茄时间(分钟)</label>
        <input class="tomato-input" type="number" id="time" v-model.number="time" min="1" max="60" required>
        <button class="tomato-button" type="submit">开始</button>
      </form>
      <div class="tomato-count">已完成 {{ count }} 个番茄</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      time: 25,
      count: 0
    }
  },
  methods: {
    submit() {
      if (this.time <= 0) {
        return;
      }
      this.$emit('start', this.time)
    }
  }
}
</script>

<style scoped>
.tomato {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.tomato-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 32px 0;
}
.tomato-title {
  font-size: 32px;
  margin-top: 16px;
}
.tomato-body {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.tomato-form {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.tomato-label {
  font-size: 16px;
  margin-bottom: 16px;
}
.tomato-input {
  width: 200px;
  height: 40px;
  font-size: 24px;
  text-align: center;
  border: 1px solid #ccc;
  margin-bottom: 16px;
}
.tomato-button {
  width: 120px;
  height: 44px;
  font-size: 18px;
  color: #fff;
  background-color: #ff5a5f;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color .3s;
}
.tomato-button:hover {
  background-color: #ff4148;
}
.tomato-count {
  margin-top: 32px;
}
</style>

2、Timer.vue 组件

计时器组件中主要是倒计时的计算和特效的显示。

<template>
  <div class="timer">
    <div class="timer-body" :class="{ 'timer-running': isRunning }">
      <div class="timer-number">{{ minutes }}</div>
      <div class="timer-colon">:</div>
      <div class="timer-number">{{ seconds }}</div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    time: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      remainingSeconds: 0,
      interval: null
    }
  },
  computed: {
    minutes() {
      return Math.floor(this.remainingSeconds / 60);
    },
    seconds() {
      const seconds = this.remainingSeconds % 60;
      return seconds < 10 ? `0${seconds}` : seconds;
    },
    isRunning() {
      return this.interval !== null
    }
  },
  created() {
    this.reset();
  },
  methods: {
    start() {
      this.interval = setInterval(() => {
        this.remainingSeconds--;
        if (this.remainingSeconds <= 0) {
          this.reset();
          this.$emit('complete');
          return;
        }
      }, 1000)
    },
    reset() {
      clearInterval(this.interval);
      this.remainingSeconds = this.time * 60;
      this.interval = null;
    }
  }
}
</script>

<style scoped>
.timer {
  display: flex;
  justify-content: center;
  align-items: center;
}
.timer-body {
  height: 120px;
  width: 220px;
  background-color: #ff5a5f;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.timer-running .timer-body {
  animation: timer 1s linear infinite;
}
.timer-number {
  font-size: 72px;
  font-weight: bold;
  color: #fff;
  text-shadow: 2px 2px 0 rgba(0, 0, 0, .2);
}
.timer-colon {
  font-size: 72px;
  margin-left: 8px;
  margin-right: 8px;
  color: #fff;
  text-shadow: 2px 2px 0 rgba(0, 0, 0, .2);
}
@keyframes timer {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.5);
  }
  100% {
    transform: scale(1);
  }
}
</style>

3、App.vue

在根组件中我们需要将蕃茄钟组件和计时器组件进行拼接。

<template>
  <div>
    <Tomato @start="startTimer" :count="toCompleteCount"></Tomato>
    <Timer v-if="isRunning" :time="currentTime" @complete="completeTomato"></Timer>
  </div>
</template>

<script>
import Tomato from './components/Tomato.vue'
import Timer from './components/Timer.vue'

export default {
  components: {
    Tomato,
    Timer
  },
  data() {
    return {
      currentTime: 0,
      completedCount: 0
    }
  },
  computed: {
    toCompleteCount() {
      return this.completedCount + 1;
    },
    isRunning() {
      return this.currentTime !== 0
    }
  },
  methods: {
    startTimer(time) {
      this.currentTime = time;
    },
    completeTomato() {
      this.completedCount++;
      this.currentTime = 0;
    }
  }
}
</script>

<style>
/* 样式省略 */
</style>

四、项目演示

下面提供两个示例:

至此,vue 实现 tomato timer(蕃茄钟)实例讲解完毕。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:vue 实现 tomato timer(蕃茄钟)实例讲解 - Python技术站

(0)
上一篇 2023年5月27日
下一篇 2023年5月27日

相关文章

  • 2022最新前端常见react面试题合集

    下面我将为您详细讲解“2022最新前端常见react面试题合集”的完整攻略,具体过程如下: 1.了解React框架 在回答React面试题之前,我们需要了解React框架的基本知识。这包括掌握React组件、虚拟DOM、生命周期函数以及Redux等内容。在掌握React框架的基础上,才能更好地回答相关的面试题。同时,也要学会使用React相关的工具,比如We…

    Vue 2023年5月28日
    00
  • vue2.0+vue-router构建一个简单的列表页的示例代码

    下面是“vue2.0+vue-router构建一个简单的列表页”的完整攻略: 步骤一:创建项目 首先,我们需要创建一个空项目,可以通过以下命令来创建: vue create my-project 创建完成后,进入项目的根目录,安装 vue-router 依赖: npm install vue-router –save 步骤二:配置路由 在 src 目录下创…

    Vue 2023年5月28日
    00
  • 浅析Vue 和微信小程序的区别、比较

    浅析Vue和微信小程序的区别、比较 Vue和微信小程序都是前端技术,目的都是为了提供更好的用户体验。但是Vue和微信小程序的开发、使用方式和运行环境等方面有所不同,下面将对它们进行简单的比较和分析。 开发方式 Vue和微信小程序的开发方式有很大的不同。 Vue使用Vue CLI和其他构建工具来创建和管理项目,使用Vue组件化开发,采用MVVM模式,提供更加灵…

    Vue 2023年5月27日
    00
  • 深度解析 Vue3 的响应式机制

    深度解析 Vue3 的响应式机制 Vue3 的响应式机制借鉴了 ES6 中的 Proxy,相较于 Vue2 中的 Object.defineProperty,其实现更加高效灵活。下面将详细介绍 Vue3 的响应式机制及其实现原理。 Vue3 的响应式机制 Vue3 中的响应式机制是通过 reactivity 模块来实现的,它主要包括以下几个部分: react…

    Vue 2023年5月27日
    00
  • vue项目配置eslint保存自动格式化问题

    下面是关于Vue项目配置ESLint保存自动格式化的完整攻略: 安装相关插件 首先,需要在Vue项目中安装eslint和eslint-plugin-prettier两个插件,可以使用npm安装,命令如下: npm install eslint eslint-plugin-prettier –save-dev 配置.eslintrc.js文件 在Vue项目的…

    Vue 2023年5月28日
    00
  • 手把手教你用VUE封装一个文本滚动组件

    让我们逐步介绍如何用Vue封装文本滚动组件。 1. 创建Vue组件 首先,我们需要创建一个Vue组件,在这个组件中实现文本滚动的功能。 <template> <div class="scroll-container"> <div class="scroll-content" ref=&qu…

    Vue 2023年5月29日
    00
  • vue 中几种传值方法(3种)

    当我们在 Vue 中需要进行组件间的数据传递时,就需要使用到一些传值方法。下面将从 props、emit 和 vuex 三个方面分别介绍 Vue 中三种传值方法。 1. props props 是 Vue 中组件间传值最常用的方法之一。它可以实现父组件向子组件传值,在子组件中可以直接使用 props 来访问。 1.1. 定义props 在父组件中定义 pro…

    Vue 2023年5月28日
    00
  • nuxt 页面路由配置,主页轮播组件开发操作

    下面我将为您详细讲解”nuxt页面路由配置,主页轮播组件开发操作”的完整攻略。 Nuxt 页面路由配置 在 Nuxt 中,页面路由可以通过 pages 目录下的目录和文件来进行定义。例如,一个名为 home.vue 的文件就可以对应主页的路由。 以下是一个页面路由的基本结构示例: pages/–| home.vue–| about/—–| inde…

    Vue 2023年5月28日
    00
合作推广
合作推广
分享本页
返回顶部