Skip to content

Instantly share code, notes, and snippets.

@radum
Forked from pankajpatel/getOverlap.js
Created October 18, 2018 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radum/3ab5b439e83e1ff20b5708294535188d to your computer and use it in GitHub Desktop.
Save radum/3ab5b439e83e1ff20b5708294535188d to your computer and use it in GitHub Desktop.
Function to get the overlap between two elements
/**
* isOverlapping() returns the overlapping status between two elements
* based on the passed in Element objects
*
* @param {Element, Element} Element object of DOM
* @return {Boolean|null} overlap status or null if native objet not received
*/
function isOverlapping(e1, e2){
if( e1.length && e1.length > 1 ){
e1 = e1[0];
}
if( e2.length && e2.length > 1 ){
e2 = e2[0];
}
var rect1 = e1 instanceof Element ? e1.getBoundingClientRect() : false;
var rect2 = e2 instanceof Element ? e2.getBoundingClientRect() : false;
window.console ? console.log(rect1, rect2 ) : null ;
var overlap = null;
if( rect1 && rect2 ){
overlap = !(
rect1.right < rect2.left ||
rect1.left > rect2.right ||
rect1.bottom < rect2.top ||
rect1.top > rect2.bottom
)
return overlap;
} else {
window.console ? console.warn( 'Please pass native Element object' ) null;
return overlap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment