Skip to content

Commit c75a449

Browse files
Don't use getComputedStyle
1 parent 0a39bee commit c75a449

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

classes/sonic-speed/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
<h1>Press an arrow key to move Sonic</h1>
1313
<img id="sonic-running" src="./assets/sonic-running.gif">
1414

15-
<!-- <script src="./script.js"></script> -->
1615
<script src="./script.js"></script>
16+
<!-- <script src="./script2.js"></script> -->
1717
</body>
1818

1919
</html>

classes/sonic-speed/script.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,28 @@
22
const stepSize = 200;
33
const sonicGif = document.getElementById('sonic-running');
44

5+
sonicGif.style.marginTop = 0;
6+
sonicGif.style.marginLeft = 0;
7+
58
// Create functions which move Sonic around page using margins
69
function moveSonicDown() {
7-
const currentMarginTop = parseInt(window.getComputedStyle(sonicGif).marginTop);
10+
const currentMarginTop = parseInt(sonicGif.style.marginTop);
811
sonicGif.style.marginTop = (currentMarginTop + stepSize) + 'px';
912
}
1013

1114
function moveSonicUp() {
12-
const currentMarginTop = parseInt(window.getComputedStyle(sonicGif).marginTop);
15+
const currentMarginTop = parseInt(sonicGif.sytle.marginTop);
1316
sonicGif.style.marginTop = (currentMarginTop - stepSize) + 'px';
1417
}
1518

1619
function moveSonicRight() {
17-
const currentMarginLeft = parseInt(window.getComputedStyle(sonicGif).marginLeft);
20+
const currentMarginLeft = parseInt(sonicGif.style.marginLeft);
21+
console.log(currentMarginLeft)
1822
sonicGif.style.marginLeft = (currentMarginLeft + stepSize) + 'px';
1923
}
2024

2125
function moveSonicLeft() {
22-
const currentMarginLeft = parseInt(window.getComputedStyle(sonicGif).marginLeft);
26+
const currentMarginLeft = parseInt(sonicGif.style.marginLeft);
2327
sonicGif.style.marginLeft = (currentMarginLeft - stepSize) + 'px';
2428
}
2529

classes/sonic-speed/script2.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy