Skip to content

Instantly share code, notes, and snippets.

@alloyking
Last active August 29, 2015 14:04
Show Gist options
  • Save alloyking/b1e4306232dd54f25bb3 to your computer and use it in GitHub Desktop.
Save alloyking/b1e4306232dd54f25bb3 to your computer and use it in GitHub Desktop.
square / rectangle entry position
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<div id="square" style="background-color: #ccc; width:200px; height:140px;">
<p id="text_direction"></p>
</div>
<script>
function getQuadrant(x, y, width, height) {
var center = {
x: x - width / 2,
y: y - height / 2
},
diag = center.x / width * height;
if (center.y < -diag && center.y < diag)
direction = "Top";
else if (center.y < -diag && center.y >= diag)
direction = "Left";
else if (center.y >= -diag && center.y >= diag)
direction = "Bottom";
else if (center.y >= -diag && center.y < diag)
direction = "Right";
else alert("failed");
return direction;
}
$('#square').mouseover(function(e) {
var squareHeight = $(this).outerHeight();
var squareWidth = $(this).outerWidth();
var vertiPos = e.pageY - this.offsetTop;
var horizPos = e.pageX - this.offsetLeft;
var pos = getQuadrant(horizPos, vertiPos, squareWidth, squareHeight);
console.log(pos);
//just dom crap here for the text that shows in the square
$('#text_direction').text(pos);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment