forked from bitovi/jquerypp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithin.js
More file actions
95 lines (82 loc) · 2.97 KB
/
within.js
File metadata and controls
95 lines (82 loc) · 2.97 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
steal('jquery', function($) {
// Checks if x and y coordinates are within a box with left, top, width and height
var withinBox = function(x, y, left, top, width, height ){
return (y >= top &&
y < top + height &&
x >= left &&
x < left + width);
}
/**
* @function jQuery.fn.within
* @parent jQuery.within
* @plugin jquerypp/dom/within
* @hide
*
* Returns all elements matching the selector that touch a given point:
*
* // get all elements that touch 200x200.
* $('*').within(200, 200);
*
* @param {Number} left the position from the left of the page
* @param {Number} top the position from the top of the page
* @param {Boolean} [useOffsetCache=false] cache the dimensions and offset of the elements.
* @return {jQuery} a jQuery collection of elements whos area
* overlaps the element position.
*/
$.fn.within= function(left, top, useOffsetCache) {
var ret = []
this.each(function(){
var q = jQuery(this);
if (this == document.documentElement) {
return ret.push(this);
}
// uses either the cached offset or .offset()
var offset = useOffsetCache ?
$.data(this,"offsetCache") || $.data(this,"offsetCache", q.offset()) :
q.offset();
// Check if the given coordinates are within the area of the current element
var res = withinBox(left, top, offset.left, offset.top,
this.offsetWidth, this.offsetHeight );
if (res) {
// Add it to the results
ret.push(this);
}
});
return this.pushStack( $.unique( ret ), "within", left+","+top );
}
/**
* @function jQuery.fn.withinBox
* @parent jQuery.within
*
* Returns all elements matching the selector that have a given area in common:
*
* $('*').withinBox(200, 200, 100, 100)
*
* @param {Number} left the position from the left of the page
* @param {Number} top the position from the top of the page
* @param {Number} width the width of the area
* @param {Number} height the height of the area
* @param {Boolean} [useOffsetCache=false] cache the dimensions and offset of the elements.
* @return {jQuery} a jQuery collection of elements whos area
* overlaps the element position.
*/
$.fn.withinBox = function(left, top, width, height, useOffsetCache){
var ret = []
this.each(function(){
var q = jQuery(this);
if(this == document.documentElement) return ret.push(this);
// use cached offset or .offset()
var offset = useOffsetCache ?
$.data(this,"offset") ||
$.data(this,"offset", q.offset()) :
q.offset();
var ew = q.width(), eh = q.height(),
// Checks if the element offset is within the given box
res = !( (offset.top > top+height) || (offset.top +eh < top) || (offset.left > left+width ) || (offset.left+ew < left));
if(res)
ret.push(this);
});
return this.pushStack( $.unique( ret ), "withinBox", $.makeArray(arguments).join(",") );
}
return $;
})