42
|
1 |
//fgnass.github.com/spin.js#v1.2.7
|
|
2 |
!function(window, document, undefined) {
|
|
3 |
|
|
4 |
/**
|
|
5 |
* Copyright (c) 2011 Felix Gnass [fgnass at neteye dot de]
|
|
6 |
* Licensed under the MIT license
|
|
7 |
*/
|
|
8 |
|
|
9 |
var prefixes = ['webkit', 'Moz', 'ms', 'O'] /* Vendor prefixes */
|
|
10 |
, animations = {} /* Animation rules keyed by their name */
|
|
11 |
, useCssAnimations
|
|
12 |
|
|
13 |
/**
|
|
14 |
* Utility function to create elements. If no tag name is given,
|
|
15 |
* a DIV is created. Optionally properties can be passed.
|
|
16 |
*/
|
|
17 |
function createEl(tag, prop) {
|
|
18 |
var el = document.createElement(tag || 'div')
|
|
19 |
, n
|
|
20 |
|
|
21 |
for(n in prop) el[n] = prop[n]
|
|
22 |
return el
|
|
23 |
}
|
|
24 |
|
|
25 |
/**
|
|
26 |
* Appends children and returns the parent.
|
|
27 |
*/
|
|
28 |
function ins(parent /* child1, child2, ...*/) {
|
|
29 |
for (var i=1, n=arguments.length; i<n; i++)
|
|
30 |
parent.appendChild(arguments[i])
|
|
31 |
|
|
32 |
return parent
|
|
33 |
}
|
|
34 |
|
|
35 |
/**
|
|
36 |
* Insert a new stylesheet to hold the @keyframe or VML rules.
|
|
37 |
*/
|
|
38 |
var sheet = function() {
|
|
39 |
var el = createEl('style', {type : 'text/css'})
|
|
40 |
ins(document.getElementsByTagName('head')[0], el)
|
|
41 |
return el.sheet || el.styleSheet
|
|
42 |
}()
|
|
43 |
|
|
44 |
/**
|
|
45 |
* Creates an opacity keyframe animation rule and returns its name.
|
|
46 |
* Since most mobile Webkits have timing issues with animation-delay,
|
|
47 |
* we create separate rules for each line/segment.
|
|
48 |
*/
|
|
49 |
function addAnimation(alpha, trail, i, lines) {
|
|
50 |
var name = ['opacity', trail, ~~(alpha*100), i, lines].join('-')
|
|
51 |
, start = 0.01 + i/lines*100
|
|
52 |
, z = Math.max(1 - (1-alpha) / trail * (100-start), alpha)
|
|
53 |
, prefix = useCssAnimations.substring(0, useCssAnimations.indexOf('Animation')).toLowerCase()
|
|
54 |
, pre = prefix && '-'+prefix+'-' || ''
|
|
55 |
|
|
56 |
if (!animations[name]) {
|
|
57 |
sheet.insertRule(
|
|
58 |
'@' + pre + 'keyframes ' + name + '{' +
|
|
59 |
'0%{opacity:' + z + '}' +
|
|
60 |
start + '%{opacity:' + alpha + '}' +
|
|
61 |
(start+0.01) + '%{opacity:1}' +
|
|
62 |
(start+trail) % 100 + '%{opacity:' + alpha + '}' +
|
|
63 |
'100%{opacity:' + z + '}' +
|
|
64 |
'}', sheet.cssRules.length)
|
|
65 |
|
|
66 |
animations[name] = 1
|
|
67 |
}
|
|
68 |
return name
|
|
69 |
}
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Tries various vendor prefixes and returns the first supported property.
|
|
73 |
**/
|
|
74 |
function vendor(el, prop) {
|
|
75 |
var s = el.style
|
|
76 |
, pp
|
|
77 |
, i
|
|
78 |
|
|
79 |
if(s[prop] !== undefined) return prop
|
|
80 |
prop = prop.charAt(0).toUpperCase() + prop.slice(1)
|
|
81 |
for(i=0; i<prefixes.length; i++) {
|
|
82 |
pp = prefixes[i]+prop
|
|
83 |
if(s[pp] !== undefined) return pp
|
|
84 |
}
|
|
85 |
}
|
|
86 |
|
|
87 |
/**
|
|
88 |
* Sets multiple style properties at once.
|
|
89 |
*/
|
|
90 |
function css(el, prop) {
|
|
91 |
for (var n in prop)
|
|
92 |
el.style[vendor(el, n)||n] = prop[n]
|
|
93 |
|
|
94 |
return el
|
|
95 |
}
|
|
96 |
|
|
97 |
/**
|
|
98 |
* Fills in default values.
|
|
99 |
*/
|
|
100 |
function merge(obj) {
|
|
101 |
for (var i=1; i < arguments.length; i++) {
|
|
102 |
var def = arguments[i]
|
|
103 |
for (var n in def)
|
|
104 |
if (obj[n] === undefined) obj[n] = def[n]
|
|
105 |
}
|
|
106 |
return obj
|
|
107 |
}
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Returns the absolute page-offset of the given element.
|
|
111 |
*/
|
|
112 |
function pos(el) {
|
|
113 |
var o = { x:el.offsetLeft, y:el.offsetTop }
|
|
114 |
while((el = el.offsetParent))
|
|
115 |
o.x+=el.offsetLeft, o.y+=el.offsetTop
|
|
116 |
|
|
117 |
return o
|
|
118 |
}
|
|
119 |
|
|
120 |
var defaults = {
|
|
121 |
lines: 12, // The number of lines to draw
|
|
122 |
length: 7, // The length of each line
|
|
123 |
width: 5, // The line thickness
|
|
124 |
radius: 10, // The radius of the inner circle
|
|
125 |
rotate: 0, // Rotation offset
|
|
126 |
corners: 1, // Roundness (0..1)
|
|
127 |
color: '#000', // #rgb or #rrggbb
|
|
128 |
speed: 1, // Rounds per second
|
|
129 |
trail: 100, // Afterglow percentage
|
|
130 |
opacity: 1/4, // Opacity of the lines
|
|
131 |
fps: 20, // Frames per second when using setTimeout()
|
|
132 |
zIndex: 2e9, // Use a high z-index by default
|
|
133 |
className: 'spinner', // CSS class to assign to the element
|
|
134 |
top: 'auto', // center vertically
|
|
135 |
left: 'auto', // center horizontally
|
|
136 |
position: 'relative' // element position
|
|
137 |
}
|
|
138 |
|
|
139 |
/** The constructor */
|
|
140 |
var Spinner = function Spinner(o) {
|
|
141 |
if (!this.spin) return new Spinner(o)
|
|
142 |
this.opts = merge(o || {}, Spinner.defaults, defaults)
|
|
143 |
}
|
|
144 |
|
|
145 |
Spinner.defaults = {}
|
|
146 |
|
|
147 |
merge(Spinner.prototype, {
|
|
148 |
spin: function(target) {
|
|
149 |
this.stop()
|
|
150 |
var self = this
|
|
151 |
, o = self.opts
|
|
152 |
, el = self.el = css(createEl(0, {className: o.className}), {position: o.position, width: 0, zIndex: o.zIndex})
|
|
153 |
, mid = o.radius+o.length+o.width
|
|
154 |
, ep // element position
|
|
155 |
, tp // target position
|
|
156 |
|
|
157 |
if (target) {
|
|
158 |
target.insertBefore(el, target.firstChild||null)
|
|
159 |
tp = pos(target)
|
|
160 |
ep = pos(el)
|
|
161 |
css(el, {
|
|
162 |
left: (o.left == 'auto' ? tp.x-ep.x + (target.offsetWidth >> 1) : parseInt(o.left, 10) + mid) + 'px',
|
|
163 |
top: (o.top == 'auto' ? tp.y-ep.y + (target.offsetHeight >> 1) : parseInt(o.top, 10) + mid) + 'px'
|
|
164 |
})
|
|
165 |
}
|
|
166 |
|
|
167 |
el.setAttribute('aria-role', 'progressbar')
|
|
168 |
self.lines(el, self.opts)
|
|
169 |
|
|
170 |
if (!useCssAnimations) {
|
|
171 |
// No CSS animation support, use setTimeout() instead
|
|
172 |
var i = 0
|
|
173 |
, fps = o.fps
|
|
174 |
, f = fps/o.speed
|
|
175 |
, ostep = (1-o.opacity) / (f*o.trail / 100)
|
|
176 |
, astep = f/o.lines
|
|
177 |
|
|
178 |
;(function anim() {
|
|
179 |
i++;
|
|
180 |
for (var s=o.lines; s; s--) {
|
|
181 |
var alpha = Math.max(1-(i+s*astep)%f * ostep, o.opacity)
|
|
182 |
self.opacity(el, o.lines-s, alpha, o)
|
|
183 |
}
|
|
184 |
self.timeout = self.el && setTimeout(anim, ~~(1000/fps))
|
|
185 |
})()
|
|
186 |
}
|
|
187 |
return self
|
|
188 |
},
|
|
189 |
|
|
190 |
stop: function() {
|
|
191 |
var el = this.el
|
|
192 |
if (el) {
|
|
193 |
clearTimeout(this.timeout)
|
|
194 |
if (el.parentNode) el.parentNode.removeChild(el)
|
|
195 |
this.el = undefined
|
|
196 |
}
|
|
197 |
return this
|
|
198 |
},
|
|
199 |
|
|
200 |
lines: function(el, o) {
|
|
201 |
var i = 0
|
|
202 |
, seg
|
|
203 |
|
|
204 |
function fill(color, shadow) {
|
|
205 |
return css(createEl(), {
|
|
206 |
position: 'absolute',
|
|
207 |
width: (o.length+o.width) + 'px',
|
|
208 |
height: o.width + 'px',
|
|
209 |
background: color,
|
|
210 |
boxShadow: shadow,
|
|
211 |
transformOrigin: 'left',
|
|
212 |
transform: 'rotate(' + ~~(360/o.lines*i+o.rotate) + 'deg) translate(' + o.radius+'px' +',0)',
|
|
213 |
borderRadius: (o.corners * o.width>>1) + 'px'
|
|
214 |
})
|
|
215 |
}
|
|
216 |
|
|
217 |
for (; i < o.lines; i++) {
|
|
218 |
seg = css(createEl(), {
|
|
219 |
position: 'absolute',
|
|
220 |
top: 1+~(o.width/2) + 'px',
|
|
221 |
transform: o.hwaccel ? 'translate3d(0,0,0)' : '',
|
|
222 |
opacity: o.opacity,
|
|
223 |
animation: useCssAnimations && addAnimation(o.opacity, o.trail, i, o.lines) + ' ' + 1/o.speed + 's linear infinite'
|
|
224 |
})
|
|
225 |
|
|
226 |
if (o.shadow) ins(seg, css(fill('#000', '0 0 4px ' + '#000'), {top: 2+'px'}))
|
|
227 |
|
|
228 |
ins(el, ins(seg, fill(o.color, '0 0 1px rgba(0,0,0,.1)')))
|
|
229 |
}
|
|
230 |
return el
|
|
231 |
},
|
|
232 |
|
|
233 |
opacity: function(el, i, val) {
|
|
234 |
if (i < el.childNodes.length) el.childNodes[i].style.opacity = val
|
|
235 |
}
|
|
236 |
|
|
237 |
})
|
|
238 |
|
|
239 |
/////////////////////////////////////////////////////////////////////////
|
|
240 |
// VML rendering for IE
|
|
241 |
/////////////////////////////////////////////////////////////////////////
|
|
242 |
|
|
243 |
/**
|
|
244 |
* Check and init VML support
|
|
245 |
*/
|
|
246 |
;(function() {
|
|
247 |
|
|
248 |
function vml(tag, attr) {
|
|
249 |
return createEl('<' + tag + ' xmlns="urn:schemas-microsoft.com:vml" class="spin-vml">', attr)
|
|
250 |
}
|
|
251 |
|
|
252 |
var s = css(createEl('group'), {behavior: 'url(#default#VML)'})
|
|
253 |
|
|
254 |
if (!vendor(s, 'transform') && s.adj) {
|
|
255 |
|
|
256 |
// VML support detected. Insert CSS rule ...
|
|
257 |
sheet.addRule('.spin-vml', 'behavior:url(#default#VML)')
|
|
258 |
|
|
259 |
Spinner.prototype.lines = function(el, o) {
|
|
260 |
var r = o.length+o.width
|
|
261 |
, s = 2*r
|
|
262 |
|
|
263 |
function grp() {
|
|
264 |
return css(
|
|
265 |
vml('group', {
|
|
266 |
coordsize: s + ' ' + s,
|
|
267 |
coordorigin: -r + ' ' + -r
|
|
268 |
}),
|
|
269 |
{ width: s, height: s }
|
|
270 |
)
|
|
271 |
}
|
|
272 |
|
|
273 |
var margin = -(o.width+o.length)*2 + 'px'
|
|
274 |
, g = css(grp(), {position: 'absolute', top: margin, left: margin})
|
|
275 |
, i
|
|
276 |
|
|
277 |
function seg(i, dx, filter) {
|
|
278 |
ins(g,
|
|
279 |
ins(css(grp(), {rotation: 360 / o.lines * i + 'deg', left: ~~dx}),
|
|
280 |
ins(css(vml('roundrect', {arcsize: o.corners}), {
|
|
281 |
width: r,
|
|
282 |
height: o.width,
|
|
283 |
left: o.radius,
|
|
284 |
top: -o.width>>1,
|
|
285 |
filter: filter
|
|
286 |
}),
|
|
287 |
vml('fill', {color: o.color, opacity: o.opacity}),
|
|
288 |
vml('stroke', {opacity: 0}) // transparent stroke to fix color bleeding upon opacity change
|
|
289 |
)
|
|
290 |
)
|
|
291 |
)
|
|
292 |
}
|
|
293 |
|
|
294 |
if (o.shadow)
|
|
295 |
for (i = 1; i <= o.lines; i++)
|
|
296 |
seg(i, -2, 'progid:DXImageTransform.Microsoft.Blur(pixelradius=2,makeshadow=1,shadowopacity=.3)')
|
|
297 |
|
|
298 |
for (i = 1; i <= o.lines; i++) seg(i)
|
|
299 |
return ins(el, g)
|
|
300 |
}
|
|
301 |
|
|
302 |
Spinner.prototype.opacity = function(el, i, val, o) {
|
|
303 |
var c = el.firstChild
|
|
304 |
o = o.shadow && o.lines || 0
|
|
305 |
if (c && i+o < c.childNodes.length) {
|
|
306 |
c = c.childNodes[i+o]; c = c && c.firstChild; c = c && c.firstChild
|
|
307 |
if (c) c.opacity = val
|
|
308 |
}
|
|
309 |
}
|
|
310 |
}
|
|
311 |
else
|
|
312 |
useCssAnimations = vendor(s, 'animation')
|
|
313 |
})()
|
|
314 |
|
|
315 |
if (typeof define == 'function' && define.amd)
|
|
316 |
define(function() { return Spinner })
|
|
317 |
else
|
|
318 |
window.Spinner = Spinner
|
|
319 |
|
|
320 |
}(window, document);
|