Created
June 15, 2012 17:59
-
-
Save Calvein/2937876 to your computer and use it in GitHub Desktop.
On the mouseenter, add a class corresponding to the axis which the mouse enter (jQuery)
This file contains hidden or 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
/** | |
* On the mouseenter, add a class corresponding to the axis which the mouse enter | |
* top-left | top | top-right | |
* left | | right | |
* bottom-left | bottom | bottom-right | |
*/ | |
var $container = $('div') | |
, width = $container.width() / 3 | |
, height = $container.height() / 3 | |
, className, x, y | |
$container | |
.on('mouseenter', function(e) { | |
className = '' | |
x = e.pageX - this.offsetLeft | |
y = e.pageY - this.offsetTop | |
// Height | |
if (y <= width) { | |
className = 'top' | |
} else if (y > width * 2) { | |
className = 'bottom' | |
} | |
// Width | |
if (x <= width) { | |
className += !className ? '' : '-' | |
className += 'left' | |
} | |
if (x > width * 2) { | |
className += !className ? '' : '-' | |
className += 'right' | |
} | |
$container.addClass(className) | |
}).on('mouseleave', function() { | |
$container.removeClass(className) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment