forked from jacoborus/nanobar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanobar.js
142 lines (131 loc) · 3.06 KB
/
nanobar.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
/* http://nanobar.micronube.com/ || https://github.com/jacoborus/nanobar/ MIT LICENSE */
(function (root) {
'use strict'
// container styles
var cssCont = {
width: '100%',
height: '4px',
zIndex: 9999,
top: '0'
}
// bar styles
var cssBar = {
width: 0,
height: '100%',
clear: 'both',
transition: 'height .3s'
}
// add `css` to `el` element
function addCss (el, css) {
var i
for (i in css) {
el.style[i] = css[i]
}
el.style.float = 'left'
}
// animation loop
function move (bar) {
var dist = bar.width - bar.here
if (dist < 0.1 && dist > -0.1) {
place(bar, bar.here)
bar.moving = false
if (bar.width === 100) {
bar.el.style.height = 0
setTimeout(function () {
bar.cont.el.removeChild(bar.el)
bar.cont.bars.splice(bar.cont.bars.indexOf(bar), 1)
}, 300)
}
} else {
place(bar, bar.width - dist / 4)
setTimeout(function () {
bar.go()
}, 16)
}
}
// set bar width
function place (bar, num) {
bar.width = num
bar.el.style.width = bar.width + '%'
}
function createBar (cont) {
// create progress element
var el = document.createElement('div')
el.style.backgroundColor = cont.opts.bg
addCss(el, cssBar)
cont.el.appendChild(el)
var bar = {
el: el,
width: 0,
here: 0,
moving: false,
cont: cont,
go: function (num) {
if (num) {
bar.here = num
if (!bar.moving) {
bar.moving = true
move(bar)
}
} else if (bar.moving) {
move(bar)
}
}
}
return bar
}
// create and insert bar in DOM and bars array
function init (cont) {
var bar = createBar(cont)
cont.bars.unshift(bar)
}
function Nanobar (opts) {
opts || (opts = {})
// set options
opts.bg = opts.bg || '#000'
var bars = [],
el = document.createElement('div'),
nanobar = {
el: el,
bars: bars,
opts: opts,
go: function (p) {
// expand bar
bars[0].go(p)
// create new bar at progress end
if (p === 100) {
init(nanobar)
}
}
}
// create bar container
if (opts.height && parseInt(opts.height, 10) > 0) {
cssCont.height = opts.height
}
// append style
addCss(el, cssCont)
if (opts.id) {
el.id = opts.id
}
// set CSS position
el.style.position = !opts.target ? 'fixed' : 'relative'
// insert container
if (!opts.target) {
document.getElementsByTagName('body')[0].appendChild(el)
} else {
opts.target.insertBefore(el, opts.target.firstChild)
}
init(nanobar)
return nanobar
}
if (typeof exports === 'object') {
// CommonJS
module.exports = Nanobar
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], Nanobar)
} else {
// Browser globals
root.Nanobar = Nanobar
}
}(this))