Skip to content

Instantly share code, notes, and snippets.

@Calvein
Created June 15, 2012 17:59
Show Gist options
  • Save Calvein/2937876 to your computer and use it in GitHub Desktop.
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)
/**
* 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