当前位置:首页 > VUE

html使用vue实现秒表

2026-02-22 11:11:08VUE

使用Vue实现秒表

以下是一个基于Vue.js实现的秒表组件,包含开始、暂停、重置功能。

创建Vue组件

<template>
  <div class="stopwatch">
    <div class="display">{{ formattedTime }}</div>
    <div class="controls">
      <button @click="start" :disabled="isRunning">开始</button>
      <button @click="pause" :disabled="!isRunning">暂停</button>
      <button @click="reset">重置</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      startTime: 0,
      elapsedTime: 0,
      timerInterval: null,
      isRunning: false
    }
  },
  computed: {
    formattedTime() {
      const date = new Date(this.elapsedTime)
      const hours = date.getUTCHours().toString().padStart(2, '0')
      const minutes = date.getUTCMinutes().toString().padStart(2, '0')
      const seconds = date.getUTCSeconds().toString().padStart(2, '0')
      const milliseconds = Math.floor(date.getUTCMilliseconds() / 10).toString().padStart(2, '0')

      return `${hours}:${minutes}:${seconds}.${milliseconds}`
    }
  },
  methods: {
    start() {
      if (!this.isRunning) {
        this.startTime = Date.now() - this.elapsedTime
        this.timerInterval = setInterval(this.updateTime, 10)
        this.isRunning = true
      }
    },
    pause() {
      if (this.isRunning) {
        clearInterval(this.timerInterval)
        this.isRunning = false
      }
    },
    reset() {
      clearInterval(this.timerInterval)
      this.elapsedTime = 0
      this.isRunning = false
    },
    updateTime() {
      this.elapsedTime = Date.now() - this.startTime
    }
  }
}
</script>

<style>
.stopwatch {
  text-align: center;
  font-family: Arial, sans-serif;
}

.display {
  font-size: 2.5em;
  margin-bottom: 20px;
}

.controls button {
  padding: 8px 16px;
  margin: 0 5px;
  font-size: 1em;
  cursor: pointer;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
</style>

使用组件

  1. 在Vue项目中注册并使用该组件:
import Stopwatch from './Stopwatch.vue'

export default {
  components: {
    Stopwatch
  }
}
  1. 在模板中使用:
<stopwatch />

功能说明

  • 开始按钮:启动计时器,从0开始计时或从暂停的位置继续计时
  • 暂停按钮:暂停当前计时,保持显示当前时间
  • 重置按钮:将计时器归零,停止任何正在进行的计时

实现原理

  • 使用Date.now()获取精确的时间戳
  • 通过setInterval每10毫秒更新一次显示时间
  • 计算经过的时间差并格式化为HH:MM:SS.ms的显示格式
  • 使用Vue的响应式数据自动更新显示

扩展功能

如需添加更多功能,如计次功能,可以扩展组件:

data() {
  return {
    // 原有数据
    laps: []
  }
},
methods: {
  // 原有方法
  recordLap() {
    this.laps.push(this.formattedTime)
  }
}

然后在模板中添加:

html使用vue实现秒表

<button @click="recordLap">计次</button>
<ul>
  <li v-for="(lap, index) in laps" :key="index">{{ lap }}</li>
</ul>

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

相关文章

vue实现访问html

vue实现访问html

Vue 访问 HTML 的方法 在 Vue 中访问或操作 HTML 元素通常涉及以下几种方式,具体选择取决于使用场景和需求。 使用 ref 获取 DOM 元素 通过 ref 属性可以直接获取模板中的…

html使用vue实现秒表

html使用vue实现秒表

使用Vue实现秒表 以下是一个基于Vue的秒表实现方案,包含开始、暂停、重置功能,并显示时、分、秒和毫秒。 核心代码实现 <template> <div class="stop…

html中的对象react如何获取

html中的对象react如何获取

React 中获取 DOM 对象的方法 在 React 中,可以通过 ref 来获取 DOM 对象或组件实例。以下是几种常见的方式: 使用 useRef Hook(函数组件推荐) import {…

html制作css

html制作css

HTML 与 CSS 基础整合 在 HTML 中引入 CSS 的方式主要有三种:内联样式、内部样式表和外部样式表。每种方式适用于不同的场景,具体选择取决于项目需求和维护性要求。 内联样式 直接在 H…

html实现js

html实现js

HTML 实现 JavaScript 的方法 在 HTML 中实现 JavaScript 可以通过多种方式,包括内联脚本、外部脚本文件以及事件处理器等。以下是几种常见的方法: 内联脚本 直接在 HT…

html实现js查找

html实现js查找

HTML 实现 JavaScript 查找功能 在 HTML 中实现 JavaScript 查找功能通常涉及监听用户输入、遍历 DOM 元素并匹配内容。以下是实现方法: 监听输入框事件 创建输入框并…