Skip to content

Commit

Permalink
Don't use getComputedStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
jackoconnordev committed Oct 21, 2023
1 parent 0a39bee commit c75a449
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion classes/sonic-speed/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<h1>Press an arrow key to move Sonic</h1>
<img id="sonic-running" src="./assets/sonic-running.gif">

<!-- <script src="./script.js"></script> -->
<script src="./script.js"></script>
<!-- <script src="./script2.js"></script> -->
</body>

</html>
Expand Down
12 changes: 8 additions & 4 deletions classes/sonic-speed/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@
const stepSize = 200;
const sonicGif = document.getElementById('sonic-running');

sonicGif.style.marginTop = 0;
sonicGif.style.marginLeft = 0;

// Create functions which move Sonic around page using margins
function moveSonicDown() {
const currentMarginTop = parseInt(window.getComputedStyle(sonicGif).marginTop);
const currentMarginTop = parseInt(sonicGif.style.marginTop);
sonicGif.style.marginTop = (currentMarginTop + stepSize) + 'px';
}

function moveSonicUp() {
const currentMarginTop = parseInt(window.getComputedStyle(sonicGif).marginTop);
const currentMarginTop = parseInt(sonicGif.sytle.marginTop);
sonicGif.style.marginTop = (currentMarginTop - stepSize) + 'px';
}

function moveSonicRight() {
const currentMarginLeft = parseInt(window.getComputedStyle(sonicGif).marginLeft);
const currentMarginLeft = parseInt(sonicGif.style.marginLeft);
console.log(currentMarginLeft)
sonicGif.style.marginLeft = (currentMarginLeft + stepSize) + 'px';
}

function moveSonicLeft() {
const currentMarginLeft = parseInt(window.getComputedStyle(sonicGif).marginLeft);
const currentMarginLeft = parseInt(sonicGif.style.marginLeft);
sonicGif.style.marginLeft = (currentMarginLeft - stepSize) + 'px';
}

Expand Down
17 changes: 17 additions & 0 deletions classes/sonic-speed/script2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const sonicImage = document.getElementById('sonic-running');

// sonicImage.style.height = '600px';
document.onkeydown = function(event) {
if (event.key === 'ArrowDown') {
sonicImage.style.marginTop = '300px';
}
if (event.key === 'ArrowUp') {
sonicImage.style.marginTop = ' 0px';
}
if (event.key === 'ArrowRight') {
sonicImage.style.marginLeft = '300px';
}
if (event.key === 'ArrowLeft') {
sonicImage.style.marginLeft = '0px';
}
}

0 comments on commit c75a449

Please sign in to comment.