1
//fgnass.github.com/spin.js#v1.2.7
2
!function(window, document, undefined) {
5
* Copyright (c) 2011 Felix Gnass [fgnass at neteye dot de]
6
* Licensed under the MIT license
9
var prefixes = ['webkit', 'Moz', 'ms', 'O'] /* Vendor prefixes */
10
, animations = {} /* Animation rules keyed by their name */
14
* Utility function to create elements. If no tag name is given,
15
* a DIV is created. Optionally properties can be passed.
17
function createEl(tag, prop) {
18
var el = document.createElement(tag || 'div')
21
for(n in prop) el[n] = prop[n]
26
* Appends children and returns the parent.
28
function ins(parent /* child1, child2, ...*/) {
29
for (var i=1, n=arguments.length; i<n; i++)
30
parent.appendChild(arguments[i])
36
* Insert a new stylesheet to hold the @keyframe or VML rules.
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
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.
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+'-' || ''
56
if (!animations[name]) {
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)
72
* Tries various vendor prefixes and returns the first supported property.
74
function vendor(el, prop) {
79
if(s[prop] !== undefined) return prop
80
prop = prop.charAt(0).toUpperCase() + prop.slice(1)
81
for(i=0; i<prefixes.length; i++) {
83
if(s[pp] !== undefined) return pp
88
* Sets multiple style properties at once.
90
function css(el, prop) {
92
el.style[vendor(el, n)||n] = prop[n]
98
* Fills in default values.
100
function merge(obj) {
101
for (var i=1; i < arguments.length; i++) {
102
var def = arguments[i]
104
if (obj[n] === undefined) obj[n] = def[n]
110
* Returns the absolute page-offset of the given element.
113
var o = { x:el.offsetLeft, y:el.offsetTop }
114
while((el = el.offsetParent))
115
o.x+=el.offsetLeft, o.y+=el.offsetTop
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
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)
145
Spinner.defaults = {}
147
merge(Spinner.prototype, {
148
spin: function(target) {
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
158
target.insertBefore(el, target.firstChild||null)
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'
167
el.setAttribute('aria-role', 'progressbar')
168
self.lines(el, self.opts)
170
if (!useCssAnimations) {
171
// No CSS animation support, use setTimeout() instead
175
, ostep = (1-o.opacity) / (f*o.trail / 100)
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)
184
self.timeout = self.el && setTimeout(anim, ~~(1000/fps))
193
clearTimeout(this.timeout)
194
if (el.parentNode) el.parentNode.removeChild(el)
200
lines: function(el, o) {
204
function fill(color, shadow) {
205
return css(createEl(), {
206
position: 'absolute',
207
width: (o.length+o.width) + 'px',
208
height: o.width + 'px',
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'
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)' : '',
223
animation: useCssAnimations && addAnimation(o.opacity, o.trail, i, o.lines) + ' ' + 1/o.speed + 's linear infinite'
226
if (o.shadow) ins(seg, css(fill('#000', '0 0 4px ' + '#000'), {top: 2+'px'}))
228
ins(el, ins(seg, fill(o.color, '0 0 1px rgba(0,0,0,.1)')))
233
opacity: function(el, i, val) {
234
if (i < el.childNodes.length) el.childNodes[i].style.opacity = val
239
/////////////////////////////////////////////////////////////////////////
240
// VML rendering for IE
241
/////////////////////////////////////////////////////////////////////////
244
* Check and init VML support
248
function vml(tag, attr) {
249
return createEl('<' + tag + ' xmlns="urn:schemas-microsoft.com:vml" class="spin-vml">', attr)
252
var s = css(createEl('group'), {behavior: 'url(#default#VML)'})
254
if (!vendor(s, 'transform') && s.adj) {
256
// VML support detected. Insert CSS rule ...
257
sheet.addRule('.spin-vml', 'behavior:url(#default#VML)')
259
Spinner.prototype.lines = function(el, o) {
260
var r = o.length+o.width
266
coordsize: s + ' ' + s,
267
coordorigin: -r + ' ' + -r
269
{ width: s, height: s }
273
var margin = -(o.width+o.length)*2 + 'px'
274
, g = css(grp(), {position: 'absolute', top: margin, left: margin})
277
function seg(i, dx, filter) {
279
ins(css(grp(), {rotation: 360 / o.lines * i + 'deg', left: ~~dx}),
280
ins(css(vml('roundrect', {arcsize: o.corners}), {
287
vml('fill', {color: o.color, opacity: o.opacity}),
288
vml('stroke', {opacity: 0}) // transparent stroke to fix color bleeding upon opacity change
295
for (i = 1; i <= o.lines; i++)
296
seg(i, -2, 'progid:DXImageTransform.Microsoft.Blur(pixelradius=2,makeshadow=1,shadowopacity=.3)')
298
for (i = 1; i <= o.lines; i++) seg(i)
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
312
useCssAnimations = vendor(s, 'animation')
315
if (typeof define == 'function' && define.amd)
316
define(function() { return Spinner })
318
window.Spinner = Spinner