-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhero.js
40 lines (36 loc) · 1.46 KB
/
hero.js
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
const heroMove = document.getElementById('hero');
document.addEventListener('keydown', function(event){
let heroPosition = parseInt(heroMove.style.left);
if(heroPosition > 0 && heroPosition < 760){
if(event.keyCode === 37){
heroPosition = heroPosition - 10;
heroMove.style.left = heroPosition + 'px';
heroMove.style.backgroundPosition = "-70px 0";
} else if (event.keyCode === 39){
heroPosition = heroPosition + 10;
heroMove.style.left = heroPosition + 'px';
heroMove.style.backgroundPosition = "-105px 0";
}
} else if (heroPosition === 0){
if(event.keyCode === 37){
heroMove.style.backgroundPosition = "-70px 0";
} else if (event.keyCode === 39){
heroPosition = heroPosition + 10;
heroMove.style.left = heroPosition + 'px';
heroMove.style.backgroundPosition = "-105px 0";
}
} else if(heroPosition === 760){
if(event.keyCode === 37){
heroPosition = heroPosition - 10;
heroMove.style.left = heroPosition + 'px';
heroMove.style.backgroundPosition = "-70px 0";
} else if (event.keyCode === 39){
heroMove.style.backgroundPosition = "-105px 0";
}
}
})
document.addEventListener('keyup',function(event){
if(event.keyCode === 37 || event.keyCode === 39){
heroMove.style.backgroundPosition = "0 0";
}
})