File tree Expand file tree Collapse file tree 3 files changed +26
-5
lines changed Expand file tree Collapse file tree 3 files changed +26
-5
lines changed Original file line number Diff line number Diff line change 12
12
< h1 > Press an arrow key to move Sonic</ h1 >
13
13
< img id ="sonic-running " src ="./assets/sonic-running.gif ">
14
14
15
- <!-- <script src="./script.js"></script> -->
16
15
< script src ="./script.js "> </ script >
16
+ <!-- <script src="./script2.js"></script> -->
17
17
</ body >
18
18
19
19
</ html >
Original file line number Diff line number Diff line change 2
2
const stepSize = 200 ;
3
3
const sonicGif = document . getElementById ( 'sonic-running' ) ;
4
4
5
+ sonicGif . style . marginTop = 0 ;
6
+ sonicGif . style . marginLeft = 0 ;
7
+
5
8
// Create functions which move Sonic around page using margins
6
9
function moveSonicDown ( ) {
7
- const currentMarginTop = parseInt ( window . getComputedStyle ( sonicGif ) . marginTop ) ;
10
+ const currentMarginTop = parseInt ( sonicGif . style . marginTop ) ;
8
11
sonicGif . style . marginTop = ( currentMarginTop + stepSize ) + 'px' ;
9
12
}
10
13
11
14
function moveSonicUp ( ) {
12
- const currentMarginTop = parseInt ( window . getComputedStyle ( sonicGif ) . marginTop ) ;
15
+ const currentMarginTop = parseInt ( sonicGif . sytle . marginTop ) ;
13
16
sonicGif . style . marginTop = ( currentMarginTop - stepSize ) + 'px' ;
14
17
}
15
18
16
19
function moveSonicRight ( ) {
17
- const currentMarginLeft = parseInt ( window . getComputedStyle ( sonicGif ) . marginLeft ) ;
20
+ const currentMarginLeft = parseInt ( sonicGif . style . marginLeft ) ;
21
+ console . log ( currentMarginLeft )
18
22
sonicGif . style . marginLeft = ( currentMarginLeft + stepSize ) + 'px' ;
19
23
}
20
24
21
25
function moveSonicLeft ( ) {
22
- const currentMarginLeft = parseInt ( window . getComputedStyle ( sonicGif ) . marginLeft ) ;
26
+ const currentMarginLeft = parseInt ( sonicGif . style . marginLeft ) ;
23
27
sonicGif . style . marginLeft = ( currentMarginLeft - stepSize ) + 'px' ;
24
28
}
25
29
Original file line number Diff line number Diff line change
1
+ const sonicImage = document . getElementById ( 'sonic-running' ) ;
2
+
3
+ // sonicImage.style.height = '600px';
4
+ document . onkeydown = function ( event ) {
5
+ if ( event . key === 'ArrowDown' ) {
6
+ sonicImage . style . marginTop = '300px' ;
7
+ }
8
+ if ( event . key === 'ArrowUp' ) {
9
+ sonicImage . style . marginTop = ' 0px' ;
10
+ }
11
+ if ( event . key === 'ArrowRight' ) {
12
+ sonicImage . style . marginLeft = '300px' ;
13
+ }
14
+ if ( event . key === 'ArrowLeft' ) {
15
+ sonicImage . style . marginLeft = '0px' ;
16
+ }
17
+ }
You can’t perform that action at this time.
0 commit comments