jquery.animate-shadow.js
6.87 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**!
* @preserve Shadow animation 1.11
* http://www.bitstorm.org/jquery/shadow-animation/
* Copyright 2011, 2013 Edwin Martin <edwin@bitstorm.org>
* Contributors: Mark Carver, Xavier Lepretre and Jason Redding
* Released under the MIT and GPL licenses.
*/
jQuery(function($, undefined) {
/**
* Check whether the browser supports RGBA color mode.
*
* Author Mehdi Kabab <http://pioupioum.fr>
* @return {boolean} True if the browser support RGBA. False otherwise.
*/
function isRGBACapable() {
var $script = $('script:first'),
color = $script.css('color'),
result = false;
if (/^rgba/.test(color)) {
result = true;
} else {
try {
result = (color !== $script.css('color', 'rgba(0, 0, 0, 0.5)').css('color'));
$script.css('color', color);
} catch (e) {
}
}
$script.removeAttr('style');
return result;
}
$.extend(true, $, {
support: {
'rgba': isRGBACapable()
}
});
/*************************************/
// First define which property to use
var styles = $('html').prop('style');
var boxShadowProperty;
$.each(['boxShadow', 'MozBoxShadow', 'WebkitBoxShadow'], function(i, property) {
var val = styles[property];
if (typeof val !== 'undefined') {
boxShadowProperty = property;
return false;
}
});
// Extend the animate-function
if (boxShadowProperty) {
$['Tween']['propHooks']['boxShadow'] = {
get: function(tween) {
return $(tween.elem).css(boxShadowProperty);
},
set: function(tween) {
var style = tween.elem.style;
var p_begin = parseShadows($(tween.elem)[0].style[boxShadowProperty] || $(tween.elem).css(boxShadowProperty));
var p_end = parseShadows(tween.end);
var maxShadowCount = Math.max(p_begin.length, p_end.length);
var i;
for(i = 0; i < maxShadowCount; i++) {
p_end[i] = $.extend({}, p_begin[i], p_end[i]);
if (p_begin[i]) {
if (!('color' in p_begin[i]) || $.isArray(p_begin[i].color) === false) {
p_begin[i].color = p_end[i].color || [0, 0, 0, 0];
}
} else {
p_begin[i] = parseShadows('0 0 0 0 rgba(0,0,0,0)')[0];
}
}
tween['run'] = function(progress) {
var rs = calculateShadows(p_begin, p_end, progress);
style[boxShadowProperty] = rs;
};
}
};
}
// Calculate an in-between shadow.
function calculateShadows(beginList, endList, pos) {
var shadows = [];
$.each(beginList, function(i) {
var parts = [], begin = beginList[i], end = endList[i];
if (begin.inset) {
parts.push('inset');
}
if (typeof end.left !== 'undefined') {
parts.push(parseFloat(begin.left + pos * (end.left - begin.left)) + 'px '
+ parseFloat(begin.top + pos * (end.top - begin.top)) + 'px');
}
if (typeof end.blur !== 'undefined') {
parts.push(parseFloat(begin.blur + pos * (end.blur - begin.blur)) + 'px');
}
if (typeof end.spread !== 'undefined') {
parts.push(parseFloat(begin.spread + pos * (end.spread - begin.spread)) + 'px');
}
if (typeof end.color !== 'undefined') {
var color = 'rgb' + ($.support['rgba'] ? 'a' : '') + '('
+ parseInt((begin.color[0] + pos * (end.color[0] - begin.color[0])), 10) + ','
+ parseInt((begin.color[1] + pos * (end.color[1] - begin.color[1])), 10) + ','
+ parseInt((begin.color[2] + pos * (end.color[2] - begin.color[2])), 10);
if ($.support['rgba']) {
color += ',' + parseFloat(begin.color[3] + pos * (end.color[3] - begin.color[3]));
}
color += ')';
parts.push(color);
}
shadows.push(parts.join(' '));
});
return shadows.join(', ');
}
// Parse the shadow value and extract the values.
function parseShadows(shadow) {
var parsedShadows = [];
var parsePosition = 0;
var parseLength = shadow.length;
function findInset() {
var m = /^inset\b/.exec(shadow.substring(parsePosition));
if (m !== null && m.length > 0) {
parsedShadow.inset = true;
parsePosition += m[0].length;
return true;
}
return false;
}
function findOffsets() {
var m = /^(-?[0-9\.]+)(?:px)?\s+(-?[0-9\.]+)(?:px)?(?:\s+(-?[0-9\.]+)(?:px)?)?(?:\s+(-?[0-9\.]+)(?:px)?)?/.exec(shadow.substring(parsePosition));
if (m !== null && m.length > 0) {
parsedShadow.left = parseInt(m[1], 10);
parsedShadow.top = parseInt(m[2], 10);
parsedShadow.blur = (m[3] ? parseInt(m[3], 10) : 0);
parsedShadow.spread = (m[4] ? parseInt(m[4], 10) : 0);
parsePosition += m[0].length;
return true;
}
return false;
}
function findColor() {
var m = /^#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/.exec(shadow.substring(parsePosition));
if (m !== null && m.length > 0) {
parsedShadow.color = [parseInt(m[1], 16), parseInt(m[2], 16), parseInt(m[3], 16), 1];
parsePosition += m[0].length;
return true;
}
m = /^#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/.exec(shadow.substring(parsePosition));
if (m !== null && m.length > 0) {
parsedShadow.color = [parseInt(m[1], 16) * 17, parseInt(m[2], 16) * 17, parseInt(m[3], 16) * 17, 1];
parsePosition += m[0].length;
return true;
}
m = /^rgb\(\s*([0-9\.]+)\s*,\s*([0-9\.]+)\s*,\s*([0-9\.]+)\s*\)/.exec(shadow.substring(parsePosition));
if (m !== null && m.length > 0) {
parsedShadow.color = [parseInt(m[1], 10), parseInt(m[2], 10), parseInt(m[3], 10), 1];
parsePosition += m[0].length;
return true;
}
m = /^rgba\(\s*([0-9\.]+)\s*,\s*([0-9\.]+)\s*,\s*([0-9\.]+)\s*,\s*([0-9\.]+)\s*\)/.exec(shadow.substring(parsePosition));
if (m !== null && m.length > 0) {
parsedShadow.color = [parseInt(m[1], 10), parseInt(m[2], 10), parseInt(m[3], 10), parseFloat(m[4])];
parsePosition += m[0].length;
return true;
}
return false;
}
function findWhiteSpace() {
var m = /^\s+/.exec(shadow.substring(parsePosition));
if (m !== null && m.length > 0) {
parsePosition += m[0].length;
return true;
}
return false;
}
function findComma() {
var m = /^\s*,\s*/.exec(shadow.substring(parsePosition));
if (m !== null && m.length > 0) {
parsePosition += m[0].length;
return true;
}
return false;
}
function normalizeShadow(shadow) {
if ($.isPlainObject(shadow)) {
var i, sColor, cLength = 0, color = [];
if ($.isArray(shadow.color)) {
sColor = shadow.color;
cLength = sColor.length;
}
for(i = 0; i < 4; i++) {
if (i < cLength) {
color.push(sColor[i]);
} else if (i === 3) {
color.push(1);
} else {
color.push(0);
}
}
}
return $.extend({
'left': 0,
'top': 0,
'blur': 0,
'spread': 0
}, shadow);
}
var parsedShadow = normalizeShadow();
while (parsePosition < parseLength) {
if (findInset()) {
findWhiteSpace();
} else if (findOffsets()) {
findWhiteSpace();
} else if (findColor()) {
findWhiteSpace();
} else if (findComma()) {
parsedShadows.push(normalizeShadow(parsedShadow));
parsedShadow = {};
} else {
break;
}
}
parsedShadows.push(normalizeShadow(parsedShadow));
return parsedShadows;
}
});