-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.position-enhanced.js
143 lines (116 loc) · 4.22 KB
/
jquery.position-enhanced.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
/*
jquery.position-enhanced plugin v0.11
---
http://github.com/benbarnett/jQuery-Position-Enhanced
http://benbarnett.net
@benpbarnett
---
Copyright (c) 2011 Ben Barnett
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---
Extends jQuery.position() to collect translated position in addition to native method
Tested with jQuery 1.3.2+
Supports -moz-transition, -webkit-transition, -o-transition, transition
Usage (exactly the same as it would be normally):
jQuery(element).position();
Changelog:
0.11 (04/11/2011):
- IE Fixes
0.10 (19/10/2011):
- Plugin created
*/
(function(jQuery, originalPositionMethod, originalOffsetMethod) {
// ----------
// Plugin variables
// ----------
var cssPrefixes = ["", "-webkit-", "-moz-", "-o-"];
// ----------
// Check if this browser supports CSS3 transitions
// ----------
var thisBody = document.body || document.documentElement,
thisStyle = thisBody.style,
transitionEndEvent = (thisStyle.WebkitTransition !== undefined) ? "webkitTransitionEnd" : (thisStyle.OTransition !== undefined) ? "oTransitionEnd" : "transitionend",
cssTransitionsSupported = thisStyle.WebkitTransition !== undefined || thisStyle.MozTransition !== undefined || thisStyle.OTransition !== undefined || thisStyle.transition !== undefined,
has3D = use3DByDefault = ('WebKitCSSMatrix' in window && 'm11' in new WebKitCSSMatrix());
/**
@private
@name translation
@function
@description Get current X and Y translations
*/
function getTranslation() {
if (!this[0]) {
return null;
}
var elem = this[0],
cStyle = window.getComputedStyle(elem, null),
translation = {
left: 0,
top: 0
};
for (var i = cssPrefixes.length - 1; i >= 0; i--){
var transform = cStyle.getPropertyValue(cssPrefixes[i] + "transform");
if (transform && (/matrix/i).test(transform)) {
var explodedMatrix = transform.replace(/^matrix\(/i, '').split(/, |\)$/g);
translation = {
left: parseInt(explodedMatrix[4], 10),
top: parseInt(explodedMatrix[5], 10)
};
break;
}
}
return translation;
}
/**
@private
@name jQuery.position
@function
@description Extended position() method to include CSS3 translations
*/
jQuery.fn.position = function() {
if (!this[0]) {
return null;
}
// bail out on Webkit/IE otherwise we double up
if (!cssTransitionsSupported || thisStyle.WebkitTransition !== undefined) return originalPositionMethod.apply(this, arguments);
var position = originalPositionMethod.apply(this, arguments),
translation = getTranslation.call(this);
return (position) ? {
left: position.left + translation.left,
top: position.top + translation.top
} : null;
};
/**
@private
@name jQuery.offset
@function
@description Extended offset() method to include CSS3 translations
*/
jQuery.fn.offset = function() {
if (!this[0]) {
return null;
}
// bail out on Webkit otherwise we double up
if (!cssTransitionsSupported || thisStyle.WebkitTransition !== undefined) return originalOffsetMethod.apply(this, arguments);
var position = originalOffsetMethod.apply(this, arguments),
translation = getTranslation.call(this);
return (position) ? {
left: position.left + translation.left,
top: position.top + translation.top
} : null;
};
})(jQuery, jQuery.fn.position, jQuery.fn.offset);