-
Notifications
You must be signed in to change notification settings - Fork 0
/
按键事件.html
86 lines (77 loc) · 2.02 KB
/
按键事件.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{width:33px;height:33px;
background-image:url(image/Actor01-Braver02.png);
position:absolute;
}
</style>
</head>
<body>
<div></div>
<script>
var div,date;
var num=0;
var bool=false;
init();
function init(){
div=document.querySelector("div");
window.addEventListener("keydown",keydownHandler);
window.addEventListener("keyup",keydownHandler);
}
function keydownHandler(e){
if(e.type==="keyup"){
bool=false;
return
}
bool=true
switch(e.keyCode){
case 37:
date=37;
div.style.backgroundPositionY=-33+"px";
// div.style.left=div.offsetLeft-5+"px";
break;
case 38:
date=38;
div.style.backgroundPositionY="0px";
// div.style.top=div.offsetTop-5+"px";
break;
case 39:
date=39;
div.style.backgroundPositionY=-66+"px";
// div.style.left=div.offsetLeft+5+"px";
break;
case 40:
date=40;
div.style.backgroundPositionY=-99+"px";
// div.style.top=div.offsetTop+5+"px";
break;
}
}
setInterval(animation,150);
function animation(){
if(!bool) return
num++;
if(num>3)num=0
switch(date){
case 37:
div.style.left=div.offsetLeft-5+"px";
break;
case 38:
div.style.top=div.offsetTop-5+"px";
break;
case 39:
div.style.left=div.offsetLeft+5+"px";
break;
case 40:
div.style.top=div.offsetTop+5+"px";
break;
}
div.style.backgroundPositionX=-num*32+"px";
}
</script>
</body>
</html>