当前位置:首页 > VUE

用vue实现打字测速

2026-02-24 01:39:13VUE

实现思路

使用Vue实现打字测速需要构建一个包含随机文本生成、用户输入比对、计时和速度计算的核心功能模块。以下是具体实现方法。

创建Vue项目

通过Vue CLI或Vite初始化项目:

用vue实现打字测速

npm init vue@latest typing-speed-test
cd typing-speed-test
npm install

核心组件结构

src/components下创建TypingTest.vue文件:

用vue实现打字测速

<template>
  <div class="typing-test">
    <h3>打字速度测试</h3>
    <div class="text-display">
      <span 
        v-for="(char, index) in textToType" 
        :key="index"
        :class="{
          'correct': index < currentPosition && userInput[index] === textToType[index],
          'incorrect': index < currentPosition && userInput[index] !== textToType[index],
          'current': index === currentPosition
        }"
      >
        {{ char }}
      </span>
    </div>
    <input 
      v-model="userInput" 
      @input="checkInput"
      :disabled="!isTestActive"
      placeholder="开始输入..."
    />
    <div v-if="showResults" class="results">
      <p>速度: {{ wpm }} WPM ({{ cpm }} CPM)</p>
      <p>准确率: {{ accuracy }}%</p>
      <button @click="resetTest">重新测试</button>
    </div>
  </div>
</template>

逻辑实现

<script>部分添加核心逻辑:

export default {
  data() {
    return {
      textToType: '',
      userInput: '',
      currentPosition: 0,
      startTime: null,
      endTime: null,
      isTestActive: false,
      showResults: false,
      wpm: 0,
      cpm: 0,
      accuracy: 0,
      sampleTexts: [
        "The quick brown fox jumps over the lazy dog",
        "Vue is a progressive framework for building user interfaces",
        "JavaScript is the programming language of the Web"
      ]
    }
  },
  mounted() {
    this.generateRandomText();
  },
  methods: {
    generateRandomText() {
      const randomIndex = Math.floor(Math.random() * this.sampleTexts.length);
      this.textToType = this.sampleTexts[randomIndex];
    },
    checkInput() {
      if (!this.isTestActive && this.userInput.length === 1) {
        this.startTest();
      }

      this.currentPosition = this.userInput.length;

      if (this.userInput === this.textToType) {
        this.endTest();
      }
    },
    startTest() {
      this.startTime = new Date();
      this.isTestActive = true;
    },
    endTest() {
      this.endTime = new Date();
      this.isTestActive = false;
      this.calculateResults();
      this.showResults = true;
    },
    calculateResults() {
      const timeInMinutes = (this.endTime - this.startTime) / 60000;
      const correctChars = [...this.userInput].filter((char, i) => 
        char === this.textToType[i]
      ).length;

      this.cpm = Math.round(correctChars / timeInMinutes);
      this.wpm = Math.round(this.cpm / 5);
      this.accuracy = Math.round((correctChars / this.textToType.length) * 100);
    },
    resetTest() {
      this.userInput = '';
      this.currentPosition = 0;
      this.showResults = false;
      this.generateRandomText();
    }
  }
}

样式设计

<style>部分添加基础样式:

.typing-test {
  max-width: 600px;
  margin: 0 auto;
  font-family: Arial, sans-serif;
}

.text-display {
  font-size: 1.2rem;
  line-height: 1.5;
  margin: 20px 0;
  padding: 10px;
  background: #f5f5f5;
  border-radius: 4px;
  min-height: 100px;
}

.correct {
  color: green;
}

.incorrect {
  color: red;
  text-decoration: underline;
}

.current {
  background-color: #e0e0e0;
}

input {
  width: 100%;
  padding: 10px;
  font-size: 1rem;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.results {
  margin-top: 20px;
  padding: 15px;
  background: #f0f8ff;
  border-radius: 4px;
}

button {
  margin-top: 10px;
  padding: 8px 16px;
  background: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

功能扩展建议

  1. 难度选择:添加不同长度的文本选项
  2. 主题切换:支持暗黑/明亮模式
  3. 历史记录:使用localStorage保存测试结果
  4. 多语言支持:增加不同语言的测试文本

使用说明

  1. 将组件导入到App.vue中
  2. 运行开发服务器:npm run dev
  3. 开始输入时自动计时
  4. 完成输入后显示速度(WPM/CPM)和准确率

该实现包含实时反馈功能,会高亮显示正确/错误的字符,并在完成测试后自动计算结果。WPM(Words Per Minute)按5个字符=1个单词的标准计算,CPM(Characters Per Minute)计算每分钟正确字符数。

标签: 测速vue
分享给朋友:

相关文章

vue设计与实现目录

vue设计与实现目录

以下是《Vue设计与实现》一书的典型目录结构(根据常见技术书籍及Vue核心内容整理): 响应式系统 响应式数据的基本原理 依赖收集与触发更新 实现简易响应式系统 Proxy与Reflec…

vue实现建模

vue实现建模

Vue 实现建模的方法 在 Vue 中实现建模通常涉及数据绑定、组件化和状态管理。以下是几种常见的方法: 数据驱动建模 Vue 的核心是数据驱动视图。通过定义数据模型,Vue 会自动更新 DOM。例…

vue observer实现

vue observer实现

Vue Observer 实现原理 Vue 的响应式系统核心是通过 Object.defineProperty(Vue 2)或 Proxy(Vue 3)实现的 Observer 模式。以下是关键实现细…

vue实现定时

vue实现定时

Vue 实现定时功能的方法 使用 setInterval 和 clearInterval 在 Vue 中可以通过 setInterval 和 clearInterval 实现定时功能。在组件的 mou…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局…

vue 实现滑动

vue 实现滑动

Vue 实现滑动效果的方法 在Vue中实现滑动效果可以通过多种方式,以下是几种常见的方法: 使用CSS过渡和动画 通过Vue的<transition>组件结合CSS过渡或动画实现滑动效果…