Skip to content

Instantly share code, notes, and snippets.

@loginx
Created November 29, 2012 23:44
Show Gist options
  • Save loginx/4172715 to your computer and use it in GitHub Desktop.
Save loginx/4172715 to your computer and use it in GitHub Desktop.
jQuery $(el).overlaps(otherEl) helper
$.fn.overlaps = (el) ->
el = $(el)
o =
el1: @offset()
el2: el.offset()
el1 =
x1: o.el1.left
x2: o.el1.left + @width()
y1: o.el1.top
y2: o.el1.top + @height()
el2 =
x1: o.el2.left
x2: o.el2.left + el.width()
y1: o.el2.top
y2: o.el2.top + el.height()
(el1.x1 < el2.x2) and (el1.x2 > el2.x1) and (el1.y1 < el2.y2) and (el1.y2 > el2.y1)
$.fn.overlaps = function(el) {
var el1, el2, o;
el = $(el);
o = {
el1: this.offset(),
el2: el.offset()
};
el1 = {
x1: o.el1.left,
x2: o.el1.left + this.width(),
y1: o.el1.top,
y2: o.el1.top + this.height()
};
el2 = {
x1: o.el2.left,
x2: o.el2.left + el.width(),
y1: o.el2.top,
y2: o.el2.top + el.height()
};
return (el1.x1 < el2.x2) && (el1.x2 > el2.x1) && (el1.y1 < el2.y2) && (el1.y2 > el2.y1);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment