Last active
April 27, 2018 16:20
-
-
Save mendes5/2e1cd5acb385479eb21facd11c56033d to your computer and use it in GitHub Desktop.
Test Collision Between two selectors ex: testColision("#DivA", ".divsB", func)
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
| const rangeIntersect = (min0, max0, min1, max1) => | |
| Math.max(min0, max0) >= Math.min(min1, max1) && Math.min(min0, max0) <= Math.max(min1, max1) | |
| const elementsIntersect = (element1, element2) => { | |
| const r0 = element1.getBoundingClientRect() | |
| const r1 = element2.getBoundingClientRect() | |
| return rangeIntersect(r0.left, r0.right, r1.left, r1.right) && rangeIntersect(r0.top, r0.bottom, r1.top, r1.bottom) | |
| } | |
| const testColision = (selector1, selector2, callback) => | |
| document.querySelectorAll(selector1).forEach(element1 => | |
| document.querySelectorAll(selector2).forEach( element2 => | |
| elementsIntersect(element1, element2) && callback(element1, element2) | |
| ) | |
| ) | |
| testColision('#div-a', '#div-b', (a, b) => console.log(a, b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment