Please see this fiddle
Ignore CSS, its not important.
The purpose of this jQuery code is to tell the direction opposite to the direction from which mouse entered inside the .container. I know its an ugly looking function but please ignore that for a while. The direction is in terms of n (for north), ne (for north-east), and so on ...
Now if you open the console and move the mouse around the div.container like a maniac, eventually you will see undefined in the console. (div.container is invisible and surrounds the circular button)
Value undefined is only possible if x === 0 && y === 0 in getMovementDirection(), which means that mouse on entering the div.container was inside the div.button (spherical button), which is not possible.
So, my question is, what's going on in that code?
-Thanks for help
PS: Title needs improvement.
jQuery code
(function($) {
var $container = $('div.container'),
$button = $('div.button');
$container.on('mouseenter', function(e) {
var mouseLocation = {
x: e.pageX,
y: e.pageY
}
var buttonLocation = {
x: $button.offset().left,
y: $button.offset().top
}
var loc = getMovementDirection (mouseLocation, buttonLocation, jQuery);
console.log(loc);
});
})(jQuery);
function getMovementDirection (mouse, container, $) {
var width = $('div.button').outerWidth(),
height = $('div.button').outerHeight();
var x, y;
if (mouse.x < container.x) { x = -1; }
else if (mouse.x < container.x + width) { x = 0; }
else { x = 1; }
if (mouse.y < container.y) { y = -1; }
else if (mouse.y < container.y + width) { y = 0; }
else { y = 1; }
if ( x === -1 && y === -1 ) { return 'se'; }
else if ( x === 0 && y === -1 ) { return 's'; }
else if ( x === 1 && y === -1 ) { return 'sw'; }
else if ( x === -1 && y === 0 ) { return 'e'; }
// x === 0 && y === 0 is forbidden
else if ( x === 1 && y === 0 ) { return 'w'; }
else if ( x === -1 && y === 1 ) { return 'ne'; }
else if ( x === 0 && y === 1 ) { return 'n'; }
else if ( x === 1 && y === 1 ) { return 'nw'; }
}