当前位置:首页 > VUE

用vue实现秒表应用

2026-02-20 17:14:00VUE

实现秒表功能

创建Vue组件Stopwatch.vue,定义数据属性time(记录时间)、isRunning(状态标识)和timer(存储计时器)。使用setInterval实现计时逻辑,通过padStart方法格式化显示时间。

用vue实现秒表应用

<template>
  <div class="stopwatch">
    <div class="display">{{ formattedTime }}</div>
    <button @click="start" :disabled="isRunning">Start</button>
    <button @click="stop" :disabled="!isRunning">Stop</button>
    <button @click="reset">Reset</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      time: 0,
      isRunning: false,
      timer: null
    }
  },
  computed: {
    formattedTime() {
      const minutes = Math.floor(this.time / 60000)
      const seconds = Math.floor((this.time % 60000) / 1000)
      const milliseconds = Math.floor((this.time % 1000) / 10)
      return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}.${milliseconds.toString().padStart(2, '0')}`
    }
  },
  methods: {
    start() {
      if (!this.isRunning) {
        this.isRunning = true
        this.timer = setInterval(() => {
          this.time += 10
        }, 10)
      }
    },
    stop() {
      if (this.isRunning) {
        clearInterval(this.timer)
        this.isRunning = false
      }
    },
    reset() {
      this.stop()
      this.time = 0
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

添加样式增强体验

在组件中添加CSS样式,优化视觉呈现和交互效果。使用Flex布局居中显示,为按钮添加悬停效果。

用vue实现秒表应用

<style scoped>
.stopwatch {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  font-family: Arial, sans-serif;
}

.display {
  font-size: 3rem;
  font-weight: bold;
  color: #333;
}

button {
  padding: 0.5rem 1rem;
  font-size: 1rem;
  cursor: pointer;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  transition: background-color 0.3s;
}

button:hover {
  background-color: #45a049;
}

button:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}
</style>

扩展功能实现分段计时

增加分段计时功能,允许用户记录多个时间点。添加laps数组存储分段数据,并实现相关UI展示。

<template>
  <div class="stopwatch">
    <!-- 原有模板内容 -->
    <button @click="recordLap" :disabled="!isRunning">Lap</button>
    <div class="laps" v-if="laps.length > 0">
      <h3>Laps:</h3>
      <ul>
        <li v-for="(lap, index) in laps" :key="index">
          Lap {{ index + 1 }}: {{ formatTime(lap) }}
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 原有数据
      laps: []
    }
  },
  methods: {
    // 原有方法
    recordLap() {
      this.laps.push(this.time)
    },
    formatTime(time) {
      // 与computed中相同的格式化逻辑
    }
  }
}
</script>

<style scoped>
/* 新增样式 */
.laps {
  margin-top: 1rem;
  width: 100%;
  max-height: 200px;
  overflow-y: auto;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  padding: 0.5rem;
  border-bottom: 1px solid #eee;
}
</style>

使用Composition API重构

对于Vue 3项目,可以使用Composition API实现更简洁的代码组织。通过setup()函数和ref响应式变量重构组件。

<script>
import { ref, computed, onBeforeUnmount } from 'vue'

export default {
  setup() {
    const time = ref(0)
    const isRunning = ref(false)
    const timer = ref(null)
    const laps = ref([])

    const formattedTime = computed(() => {
      const minutes = Math.floor(time.value / 60000)
      const seconds = Math.floor((time.value % 60000) / 1000)
      const milliseconds = Math.floor((time.value % 1000) / 10)
      return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}.${milliseconds.toString().padStart(2, '0')}`
    })

    const start = () => {
      if (!isRunning.value) {
        isRunning.value = true
        timer.value = setInterval(() => {
          time.value += 10
        }, 10)
      }
    }

    const stop = () => {
      if (isRunning.value) {
        clearInterval(timer.value)
        isRunning.value = false
      }
    }

    const reset = () => {
      stop()
      time.value = 0
      laps.value = []
    }

    const recordLap = () => {
      laps.value.push(time.value)
    }

    onBeforeUnmount(() => {
      clearInterval(timer.value)
    })

    return {
      time,
      isRunning,
      laps,
      formattedTime,
      start,
      stop,
      reset,
      recordLap
    }
  }
}
</script>

标签: 秒表vue
分享给朋友:

相关文章

vue实现全局遮罩层

vue实现全局遮罩层

实现全局遮罩层的方法 在Vue中实现全局遮罩层可以通过多种方式完成,以下是几种常见的实现方法: 使用Vue组件创建遮罩层 创建一个遮罩层组件,通过全局注册或动态挂载的方式实现全局调用。以下是一个简单…

vue的艾特功能实现

vue的艾特功能实现

Vue 的 @ 功能实现 在 Vue 中实现类似社交平台的 @ 功能,通常涉及输入框的监听、用户匹配和选择插入。以下是具体实现方法: 监听输入框内容 使用 v-model 绑定输入框内容,并通过 @…

vue实现帖子

vue实现帖子

Vue 实现帖子功能 使用 Vue 实现帖子功能需要结合前端框架和后端数据交互,以下是实现的关键步骤和代码示例。 帖子列表展示 创建帖子列表组件,使用 v-for 渲染帖子数据,通过 axios 获…

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &l…

vue实现异步

vue实现异步

Vue 实现异步操作的常见方法 Vue 提供了多种方式来处理异步操作,包括数据获取、事件处理等场景。 使用 async/await 在 Vue 方法中可以直接使用 async/await 语法处理异…

vue 实现fadeout

vue 实现fadeout

Vue 实现淡出效果 在 Vue 中实现淡出(fade-out)效果可以通过 CSS 过渡或动画结合 Vue 的动态渲染功能完成。以下是几种常见方法: 使用 Vue 过渡(Transition)组件…