Created
July 13, 2016 01:13
-
-
Save outoftime/f303a26f1f9c3d1db467607968ea154c to your computer and use it in GitHub Desktop.
Exported from Popcode. Click to import: https://popcode.org/?gist=f303a26f1f9c3d1db467607968ea154c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Page Title</title> | |
</head> | |
<body> | |
<!-- Put your page markup here --> | |
<div class="box" id="cursor"></div> | |
<div class="box" id="target"></div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $cursor = $('#cursor'), $target = $('#target'); | |
function checkCollision() { | |
var cursorLeft = $cursor.offset().left; | |
var targetLeft = $target.offset().left; | |
var cursorRight = cursorLeft + $cursor.width(); | |
var targetRight = targetLeft + $target.width(); | |
if(cursorRight > targetLeft && cursorLeft < targetRight) { | |
alert('Crash!'); | |
} | |
} | |
$('body').keydown(function(e) { | |
if (e.which === 37) { | |
$cursor.css('left', $cursor.offset().left - 10); | |
} else if (e.which === 39) { | |
$cursor.css('left', $cursor.offset().left + 10); | |
} else { | |
return; | |
} | |
checkCollision(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.box { | |
height: 100px; | |
width: 100px; | |
position: absolute; | |
} | |
#cursor { | |
background-color: rgba(0, 0, 255, 0.7); | |
z-index: 1; | |
} | |
#target { | |
background-color: red; | |
left: 50%; | |
margin-left: -50px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment