This repository has been archived by the owner on Jul 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.55_utils.js
188 lines (169 loc) · 6.54 KB
/
jquery.55_utils.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright 2012 55 Minutes (http://www.55minutes.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//= require ./55_utils
/*------------------------------------------------------------------------------
| jquery.55_utils.js
| 55 Minutes JS utilities v5.0
| Author(s): Richa Avasthi
| Created: 2009-12-02
|
| Some jQuery-based JavaScript utilities.
------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
| dimensions(element, options)
|
| Return the width and height of the given element. If the "includeAll" flag is
| set to true, then return the outer width and height (including padding, border
| and margin) of the element. The "includePaddingAndBorder" flag includes
| padding and border, but not margin. NOTE that this function operates on the
| first matched element, and returns not the jQuery object, but an object
| containing the dimensions of the element.
------------------------------------------------------------------------------*/
jQuery.fn.dimensions = function(options) {
var settings = jQuery.extend({
includeAll: false,
includePaddingAndBorder: false,
includePadding: false
}, options);
var element = this.eq(0);
// Include margin, border and padding
if(settings.includeAll)
{
return {
width: element.outerWidth(true),
height: element.outerHeight(true)
};
}
// Include border and padding
if(settings.includePaddingAndBorder)
{
return {
width: element.outerWidth(),
height: element.outerHeight()
};
}
// Include padding only
if(settings.includePadding)
{
return {
width: element.innerWidth(),
height: element.innerHeight()
};
}
// Do not include margin, border or padding
return {
width: element.width(),
height: element.height()
};
};
/*------------------------------------------------------------------------------
| getPositionedParentOffset(element)
|
| Return the offset of the given element's nearest positioned ancestor,
| providing that the ancestor is not the body element. NOTE that this function
| operates on the first matched element, and returns not the jQuery object, but
| an object containing the offset of the element's nearest positioned parent.
| Requirements: 55_utils
------------------------------------------------------------------------------*/
jQuery.fn.getPositionedParentOffset = function() {
var parentOffset = { left: 0, top: 0 };
var parent = this.eq(0).offsetParent();
// If the given element exists and it's not the body element
if(parent.length > 0 && parent.get(0).tagName.toLowerCase() != "body")
{
parentOffset = parent.offset();
// If this browser is IE7, make sure to subtract the scroll offsets out.
if(isIE(7))
{
parentOffset.left -= parent.scrollLeft();
parentOffset.top -= parent.scrollTop();
}
}
return parentOffset;
};
/*------------------------------------------------------------------------------
| isWithinBounds(position, element)
|
| position: A dictionary containing the coordinates, structured as follows:
| {x: ##, y: ##}
| element: A DOM element
|
| Return true if the coordinates given are within the bounds of the specified
| element.
------------------------------------------------------------------------------*/
function isWithinBounds(position, element)
{
element = jQuery(element);
var elementOffset = element.offset();
var elementDimensions = element.dimensions({ includeAll: true });
return (position.y >= elementOffset.top &&
position.y <= elementOffset.top + elementDimensions.height &&
position.x >= elementOffset.left &&
position.x <= elementOffset.left + elementDimensions.width);
}
/*------------------------------------------------------------------------------
| viewportCoordinates()
|
| Return the coordinates of the top, right, bottom and left edges of the
| viewport relative to the page.
------------------------------------------------------------------------------*/
function viewportCoordinates()
{
var viewport = jQuery(window);
var viewportTop = viewport.scrollTop();
var viewportLeft = viewport.scrollLeft();
return {
top: viewportTop,
bottom: viewportTop + viewport.height(),
left: viewportLeft,
right: viewportLeft + viewport.width()
};
}
/*------------------------------------------------------------------------------
| viewportOffset(element)
|
| Return the coordinates of four edges of the given element with respect to the
| viewport. NOTE that this function operates on the first matching element and
| returns not the jQuery object but a dictionary containing the viewport offset
| coordinates of the element.
------------------------------------------------------------------------------*/
jQuery.fn.viewportOffset = function() {
var offset = this.eq(0).offset();
var vcoords = viewportCoordinates();
return {
top: offset.top - vcoords.top,
bottom: offset.top + this.outerHeight() - vcoords.bottom,
left: offset.left - vcoords.left,
right: offset.left + this.outerWidth() - vcoords.right
};
};
/*------------------------------------------------------------------------------
| outsideViewport(element)
|
| Return boolean values indicating whether each of the four edges of the given
| element is outside of the viewport. NOTE that this function operates on the
| first matching element and returns not the jQuery object but a dictionary
| containing the viewport collision status of each edge of the element.
------------------------------------------------------------------------------*/
jQuery.fn.outsideViewport = function() {
var vOffset = this.eq(0).viewportOffset();
return {
top: vOffset.top < 0,
right: vOffset.right > 0,
bottom: vOffset.bottom > 0,
left: vOffset.left < 0
};
};