-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.cover.js
72 lines (61 loc) · 2.04 KB
/
jquery.cover.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*! 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.cover = function(selector) {
return this.pushStack(filterCover(this, selector && $(selector)));
};
function filterCover(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 (checkCover(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 checkCover(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 x1 >= x2 && (x2 + w2) >= (x1 + w1) &&
y1 >= y2 && (y2 + h2) >= (y1 + h1);
}
}));