效果图:
需求分析:
- 在char这个div里面显示要输入的字母,大写
- 在result这个div里面时时显示正确率,比如98%
- 给文档绑定按键事件
- 如果输入的内容和char里面一致,显示正确动画:animated zoomIn,并更换输入的字母
- 如果输入的内容和char里面不一致,显示错误动画:animated shake error
- 不管是正确还是错误都时时更新result里面的正确率
源代码:
<mian>
<div id="char">Adiv>
<div id="result">请在按键上按下屏幕上显示的字母div>
mian>
- 为了实现一些特效,先创建一个animate.css文件,在封装一些动画效果放里面
.animated {
-webkit-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}
.animated.infinite {
-webkit-animation-iteration-count: infinite;
animation-iteration-count: infinite;
}
.animated.hinge {
-webkit-animation-duration: 2s;
animation-duration: 2s;
}
.animated.flipOutX,
.animated.flipOutY,
.animated.bounceIn,
.animated.bounceOut {
-webkit-animation-duration: .75s;
animation-duration: .75s;
}
@-webkit-keyframes zoomIn {
from {
opacity: 0;
-webkit-transform: scale3d(.3, .3, .3);
transform: scale3d(.3, .3, .3);
}
50% {
opacity: 1;
}
}
@keyframes zoomIn {
from {
opacity: 0;
-webkit-transform: scale3d(.3, .3, .3);
transform: scale3d(.3, .3, .3);
}
50% {
opacity: 1;
}
}
.zoomIn {
-webkit-animation-name: zoomIn;
animation-name: zoomIn;
}
.animated {
-webkit-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}
@-webkit-keyframes shake {
from, to {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
10%, 30%, 50%, 70%, 90% {
-webkit-transform: translate3d(-10px, 0, 0);
transform: translate3d(-10px, 0, 0);
}
20%, 40%, 60%, 80% {
-webkit-transform: translate3d(10px, 0, 0);
transform: translate3d(10px, 0, 0);
}
}
@keyframes shake {
from, to {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
10%, 30%, 50%, 70%, 90% {
-webkit-transform: translate3d(-10px, 0, 0);
transform: translate3d(-10px, 0, 0);
}
20%, 40%, 60%, 80% {
-webkit-transform: translate3d(10px, 0, 0);
transform: translate3d(10px, 0, 0);
}
}
.shake {
-webkit-animation-name: shake;
animation-name: shake;
}
- css主体代码,无动画特效版
<style>
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
background: radial-gradient(circle, #444, #111, #000);
}
#char {
font-size: 400px;
color: lightgreen;
text-shadow: 0 0 50px #666;
}
#result {
font-size: 20px;
color: #888;
}
#char.error {
color: red;
}
style>
- 为了简化代码,先封装一些常用的自定义构造函数
<script>
function rand(min, max) {
return parseInt(Math.random() * (max - min + 1)) + min;
}
script>
- js主体部分,需要用到封装的函数,调用即可
<script>
var charDiv = document.getElementById('char');
var resultDiv = document.getElementById('result');
var code, tirme;
var rightNum = 0;
var wrongNum = 0;
showChar();
document.onkeyup = function (e) {
e = window.event || e;
var keyCode = e.keyCode || e.which;
if (keyCode == code) {
charDiv.className = "animated zoomIn";
rightNum++;
showChar()
}
else {
charDiv.className = "animated shake error";
wrongNum++
}
setTimeout(function () {
charDiv.className = "";
}, 500)
resultDiv.innerHTML = "正确率:" + parseInt(rightNum / (rightNum + wrongNum) * 100) + "%"
}
function showChar() {
code = rand(65, 90);
var char = String.fromCharCode(code);
charDiv.innerHTML = char;
}
script>
<html>
<head>
<meta charset="utf-8">
<title>打字游戏title>
<link rel="stylesheet" href="animate.css">
<style>
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
background: radial-gradient(circle, #444, #111, #000);
}
#char {
font-size: 400px;
color: lightgreen;
text-shadow: 0 0 50px #666;
}
#result {
font-size: 20px;
color: #888;
}
#char.error {
color: red;
}
style>
head>
<body>
<mian>
<div id="char">Adiv>
<div id="result">请在按键上按下屏幕上显示的字母div>
mian>
body>
html>
<script>
function rand(min, max) {
return parseInt(Math.random() * (max - min + 1)) + min;
}
script>
<script>
var charDiv = document.getElementById('char');
var resultDiv = document.getElementById('result');
var code, tirme;
var rightNum = 0;
var wrongNum = 0;
showChar();
document.onkeyup = function (e) {
e = window.event || e;
var keyCode = e.keyCode || e.which;
if (keyCode == code) {
charDiv.className = "animated zoomIn";
rightNum++;
showChar()
}
else {
charDiv.className = "animated shake error";
wrongNum++
}
setTimeout(function () {
charDiv.className = "";
}, 500)
resultDiv.innerHTML = "正确率:" + parseInt(rightNum / (rightNum + wrongNum) * 100) + "%"
}
function showChar() {
code = rand(65, 90);
var char = String.fromCharCode(code);
charDiv.innerHTML = char;
}
script>