当前位置:首页 > VUE

vue实现打字练习功能

2026-02-21 21:27:47VUE

实现思路

在Vue中实现打字练习功能,需要构建一个包含文本显示、用户输入比对和实时反馈的交互系统。核心在于监听键盘事件、比对输入内容与目标文本,并动态更新UI。

vue实现打字练习功能

核心代码实现

模板部分

<template>
  <div class="typing-container">
    <div class="target-text" ref="targetText">
      <span 
        v-for="(char, index) in targetChars" 
        :key="index"
        :class="{
          'correct': index < currentPosition && char === userInput[index],
          'incorrect': index < currentPosition && char !== userInput[index],
          'current': index === currentPosition
        }"
      >
        {{ char }}
      </span>
    </div>
    <input 
      v-model="userInput" 
      @input="checkInput"
      @keydown.delete="handleBackspace"
      ref="inputField"
      class="typing-input"
    />
    <div v-if="showResult" class="result">
      正确率: {{ accuracy }}% | 用时: {{ timeUsed }}秒
    </div>
  </div>
</template>

脚本部分

<script>
export default {
  data() {
    return {
      targetText: "The quick brown fox jumps over the lazy dog",
      userInput: "",
      currentPosition: 0,
      startTime: null,
      endTime: null,
      showResult: false
    };
  },
  computed: {
    targetChars() {
      return this.targetText.split("");
    },
    accuracy() {
      if (this.userInput.length === 0) return 0;
      let correct = 0;
      for (let i = 0; i < this.userInput.length; i++) {
        if (this.userInput[i] === this.targetChars[i]) correct++;
      }
      return Math.round((correct / this.userInput.length) * 100);
    },
    timeUsed() {
      if (!this.startTime) return 0;
      const end = this.endTime || new Date();
      return ((end - this.startTime) / 1000).toFixed(2);
    }
  },
  methods: {
    checkInput() {
      if (!this.startTime) this.startTime = new Date();

      if (this.userInput.length > this.targetText.length) {
        this.userInput = this.userInput.slice(0, this.targetText.length);
      }

      this.currentPosition = this.userInput.length;

      if (this.userInput === this.targetText) {
        this.endTime = new Date();
        this.showResult = true;
      }
    },
    handleBackspace(e) {
      if (this.userInput.length === 0) {
        e.preventDefault();
      }
    },
    focusInput() {
      this.$refs.inputField.focus();
    }
  },
  mounted() {
    this.focusInput();
  }
};
</script>

样式部分

<style>
.typing-container {
  max-width: 800px;
  margin: 0 auto;
  font-family: monospace;
}

.target-text {
  font-size: 24px;
  line-height: 1.5;
  margin-bottom: 20px;
  white-space: pre-wrap;
}

.target-text span {
  position: relative;
}

.target-text span.correct {
  color: #4CAF50;
}

.target-text span.incorrect {
  color: #F44336;
  text-decoration: underline;
}

.target-text span.current {
  background-color: #FFEB3B;
}

.typing-input {
  width: 100%;
  padding: 10px;
  font-size: 16px;
  opacity: 0;
  position: absolute;
}

.result {
  margin-top: 20px;
  font-size: 18px;
}
</style>

功能扩展建议

随机文本生成

可以预置多个练习文本或连接API获取随机文本:

vue实现打字练习功能

const sampleTexts = [
  "Programming is the art of telling another human what one wants the computer to do.",
  "The best way to predict the future is to invent it.",
  "Debugging is twice as hard as writing the code in the first place."
];

// 在data中替换targetText初始化
targetText: sampleTexts[Math.floor(Math.random() * sampleTexts.length)]

难度分级

根据用户水平调整文本长度和复杂度:

const difficultyLevels = {
  easy: "Short sentences with common words",
  medium: "Longer passages with technical terms",
  hard: "Complex sentences with punctuation"
};

实时数据统计

添加WPM(每分钟字数)计算:

computed: {
  wpm() {
    if (!this.startTime || !this.userInput) return 0;
    const words = this.userInput.trim().split(/\s+/).length;
    const minutes = (new Date() - this.startTime) / 60000;
    return Math.round(words / minutes);
  }
}

注意事项

  • 使用monospace字体保证字符对齐
  • 隐藏实际输入框,通过虚拟显示提升体验
  • 处理退格键防止光标位置错误
  • 组件销毁时移除事件监听
  • 移动端需要考虑虚拟键盘的适配问题

实现过程中可以通过添加动画效果、音效反馈等方式增强用户体验。对于高级功能,可以考虑加入用户登录保存练习记录、错题统计等功能。

标签: 练习功能
分享给朋友:

相关文章

vue实现用户添加功能

vue实现用户添加功能

实现用户添加功能的步骤 表单设计与数据绑定 在Vue组件中设计一个表单,包含用户名、邮箱、密码等字段。使用v-model指令实现双向数据绑定,将表单输入与组件的数据属性关联起来。 <templ…

uniapp实现支付功能

uniapp实现支付功能

支付功能实现概述 在UniApp中实现支付功能通常需要对接第三方支付平台(如微信支付、支付宝支付等)。以下是基于微信支付和支付宝支付的通用实现流程。 微信支付实现步骤 1. 配置支付权限 在微信开放…

vue实现换页功能

vue实现换页功能

Vue 实现分页功能 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 在 main.js 中引入: import Vue fr…

vue各种功能实现

vue各种功能实现

Vue 基础功能实现 数据绑定与响应式更新 使用 v-model 实现表单元素与数据的双向绑定,数据变更自动更新视图。 示例: <input v-model="message"> &…

php实现登录功能

php实现登录功能

创建登录表单 在HTML中创建一个表单,包含用户名和密码输入框。表单的action属性指向处理登录的PHP文件,method设置为POST。 <form action="login.php"…

php实现回复功能

php实现回复功能

实现回复功能的基本步骤 在PHP中实现回复功能通常涉及数据库操作、表单处理和前后端交互。以下是实现回复功能的关键步骤: 数据库设计 创建包含回复数据的表结构,例如: CREATE TABLE re…