-
-
Save directionforward/e7d76671a00ae861d821e92057463939 to your computer and use it in GitHub Desktop.
Detect if two elements are colliding/overlapping
This file contains 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
var is_colliding = function($div1, $div2) { | |
// Div1 data | |
var d1_offset = $div1.offset(); | |
var d1_height = $div1.outerHeight( true ); | |
var d1_width = $div1.outerWidth( true ); | |
var d1_distance_from_top = d1_offset.top + d1_height; | |
var d1_distance_from_left = d1_offset.left + d1_width; | |
// Div2 data | |
var d2_offset = $div2.offset(); | |
var d2_height = $div2.outerHeight( true ); | |
var d2_width = $div2.outerWidth( true ); | |
var d2_distance_from_top = d2_offset.top + d2_height; | |
var d2_distance_from_left = d2_offset.left + d2_width; | |
var not_colliding = ( d1_distance_from_top < d2_offset.top || d1_offset.top > d2_distance_from_top || d1_distance_from_left < d2_offset.left || d1_offset.left > d2_distance_from_left ); | |
// Return true/false | |
return !not_colliding; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment