Skip to content

Instantly share code, notes, and snippets.

@dursk
Created May 9, 2017
Embed
What would you like to do?
Exported from Popcode. Click to import: https://popcode.org/?gist=7280140ba8c0b3ad124e699429717984
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div id="game">
<div id="bar"></div>
</div>
</body>
</html>
{"enabledLibraries":["jquery"]}
var position = $("#bar").offset().left;
$("body").keydown(function(event) {
if (event.which === 37 && position > 13) {
position = position - 10;
$("#bar").css("left", position);
} else if (event.which === 39 && position < 483) {
position = position + 10;
$("#bar").css("left", position);
} else {
return;
}
});
#game {
border: 5px solid black;
height: 500px;
width: 500px;
}
#bar {
background-color: black;
height: 8px;
width: 30px;
position: absolute;
margin-top: 480px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment