Created
May 2, 2014 19:44
-
-
Save fastmover/078a12d651e2bf15cffa to your computer and use it in GitHub Desktop.
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
| /*! Copyright (c) 2010 Brandon Aaron (http://brandonaaron.net) | |
| * Licensed under the MIT License (LICENSE.txt). | |
| * | |
| * Version 1.2.3 | |
| */ | |
| (function (factory) { | |
| if (typeof define === 'function' && define.amd) { | |
| // AMD. Register as an anonymous module. | |
| define(['jquery'], factory); | |
| } else { | |
| // Browser globals | |
| factory(jQuery); | |
| } | |
| }(function ($) { | |
| $.fn.overlaps = function(selector) { | |
| return this.pushStack(filterOverlaps(this, selector && $(selector))); | |
| }; | |
| $.fn.notoverlaps = function(selector) { | |
| return this.pushStack(filterNotOverlaps(this, selector && $(selector))); | |
| }; | |
| function filterOverlaps(collection1, collection2) { | |
| var dims1 = getDims(collection1), | |
| dims2 = !collection2 ? dims1 : getDims(collection2), | |
| stack = [], | |
| index1 = 0, | |
| index2 = 0, | |
| length1 = dims1.length, | |
| length2 = !collection2 ? dims1.length : dims2.length; | |
| if (!collection2) { collection2 = collection1; } | |
| for (; index1 < length1; index1++) { | |
| for (index2 = 0; index2 < length2; index2++) { | |
| if (collection1[index1] === collection2[index2]) { | |
| continue; | |
| } else if (checkOverlap(dims1[index1], dims2[index2])) { | |
| stack.push( (length1 > length2) ? | |
| collection1[index1] : | |
| collection2[index2]); | |
| } | |
| } | |
| } | |
| return $.unique(stack); | |
| } | |
| function filterNotOverlaps(collection1, collection2) { | |
| var dims1 = getDims(collection1), | |
| dims2 = !collection2 ? dims1 : getDims(collection2), | |
| stack = [], | |
| index1 = 0, | |
| index2 = 0, | |
| length1 = dims1.length, | |
| length2 = !collection2 ? dims1.length : dims2.length; | |
| if (!collection2) { collection2 = collection1; } | |
| for (; index1 < length1; index1++) { | |
| for (index2 = 0; index2 < length2; index2++) { | |
| if (collection1[index1] === collection2[index2]) { | |
| continue; | |
| } else if (!checkOverlap(dims1[index1], dims2[index2])) { | |
| stack.push( (length1 > length2) ? | |
| collection1[index1] : | |
| collection2[index2]); | |
| } | |
| } | |
| } | |
| return $.unique(stack); | |
| } | |
| function getDims(elems) { | |
| var dims = [], i = 0, offset, elem; | |
| while ((elem = elems[i++])) { | |
| offset = $(elem).offset(); | |
| dims.push([ | |
| offset.top, | |
| offset.left, | |
| elem.offsetWidth, | |
| elem.offsetHeight | |
| ]); | |
| } | |
| return dims; | |
| } | |
| function checkOverlap(dims1, dims2) { | |
| var x1 = dims1[1], y1 = dims1[0], | |
| w1 = dims1[2], h1 = dims1[3], | |
| x2 = dims2[1], y2 = dims2[0], | |
| w2 = dims2[2], h2 = dims2[3]; | |
| return !(y2 + h2 <= y1 || y1 + h1 <= y2 || x2 + w2 <= x1 || x1 + w1 <= x2); | |
| } | |
| })); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment