MooTools - Detect from which side the mouse entered a div

 

var NaMooGetDir = new Class({initialize: function(){this.box =$(box);this.box.addEvents({mouseover: function(e){var side = this.getDir(this.box,e);$(test).set(text,side);}.bind(this),});},getDir: function(elem,e) {var cord =elem.getCoordinates(); var w = cord.width; var h = cord.height; var left = cord.left; var top = cord.top; var x = (e.page.x - left - (w/2)) * ( w > h ? (h/w) : 1 ); var y = (e.page.y - top - (h/2)) * ( h > w ? (w/h) : 1 ); var direction = Math.round((((Math.atan2(y, x) * (180 / Math.PI)) + 180 ) / 90 ) + 3 ) % 4; switch(direction) { case 0: return top; break; case 1: return right; break; case 2: return bottom; break; case 3: return left; break; }}});