当前位置:首页 > 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实现答题功能的基本思路 在Vue中实现答题功能通常需要以下几个核心模块:题目数据管理、用户交互处理、答题状态跟踪和结果计算。通过组件化开发可以更好地组织代码结构。 数据结构设计 答题功能的基础…

vue实现筛选功能

vue实现筛选功能

实现筛选功能的基本思路 在Vue中实现筛选功能通常涉及数据绑定、计算属性和方法的使用。通过监听用户输入或选择,动态过滤数据列表。 基础筛选实现 创建一个输入框绑定到Vue实例的data属性,使用计算…

js实现复制功能实现

js实现复制功能实现

使用document.execCommand方法(传统方式,已逐渐被弃用) 传统方式通过document.execCommand('copy')实现复制功能,需先选中文本。以下是一个完整示例: fu…

vue实现预览功能

vue实现预览功能

Vue 实现文件预览功能 使用 FileReader 实现本地文件预览 通过 HTML5 的 FileReader API 可以读取本地文件内容并显示预览。创建一个文件上传组件,监听 change…

vue答题功能实现

vue答题功能实现

Vue 答题功能实现 数据结构设计 使用 Vue 的响应式数据管理题目和答案。通常需要一个数组存储题目列表,每个题目包含题干、选项、正确答案等字段。 data() { return {…

at 功能实现vue

at 功能实现vue

实现 Vue 中的 @ 功能 在 Vue 中实现类似社交平台的 @ 提及功能,通常需要结合输入框(如 <textarea> 或可编辑的 <div>)和用户列表的交互。以下是具体…