├── ProBar.js ├── index.html └── readme.md /ProBar.js: -------------------------------------------------------------------------------- 1 | (function (root) { 2 | "use strict"; 3 | 4 | //////////////////// 5 | // all functions. // 6 | //////////////////// 7 | // first class instantiation. 8 | function proBar(options){ 9 | 10 | this.height = 5; // 5px by default. 11 | this.colorBar = "#2a2a2a"; 12 | this.wrapper_color = "#ecf0f1"; 13 | this.speedAnimation = 0.3; // in seconds 14 | this.wrapper = "body"; // default value of appending. 15 | this.finishAnimation = true; 16 | this.classNameBar = "progressBar"; // default. 17 | this.wrapperId = "wrapper-progressBar"; // default. 18 | this.rounded = { // by default All is on Zero. 19 | topLeft : 0, 20 | topRight : 0, 21 | bottomLeft : 0, 22 | bottomRight : 0 23 | }; 24 | this.roundedInternal = { // by default All is on Zero. 25 | topLeft : 0, 26 | topRight : 0, 27 | bottomLeft : 0, 28 | bottomRight : 0 29 | } 30 | 31 | this.options = options || {}; 32 | 33 | 34 | if(this.options.color) { this.colorBar = this.options.color } 35 | if(this.options.height) { 36 | this.height = this.options.height; 37 | } 38 | if(this.options.bgColor) { this.wrapper_color = this.options.bgColor } 39 | if(this.options.speed) { this.speedAnimation = this.options.speed } 40 | if(this.options.wrapper) { this.wrapper = this.options.wrapper } 41 | if(this.options.finishAnimation == false) { this.finishAnimation = this.options.finishAnimation;} 42 | if(this.options.classNameBar) { this.classNameBar = this.options.classNameBar } 43 | if(this.options.wrapperId) { this.wrapperId = this.options.wrapperId } 44 | if(this.options.rounded) { 45 | this.rounded.topLeft = this.options.rounded.topLeft || 0; 46 | this.rounded.topRight = this.options.rounded.topRight || 0; 47 | this.rounded.bottomLeft = this.options.rounded.bottomLeft || 0; 48 | this.rounded.bottomRight = this.options.rounded.bottomRight || 0; 49 | } 50 | if(this.options.roundedInternal) { 51 | this.roundedInternal.topLeft = this.options.roundedInternal.topLeft || 0; 52 | this.roundedInternal.topRight = this.options.roundedInternal.topRight || 0; 53 | this.roundedInternal.bottomLeft = this.options.roundedInternal.bottomLeft || 0; 54 | this.roundedInternal.bottomRight = this.options.roundedInternal.bottomRight || 0; 55 | } 56 | 57 | // create Bar. 58 | createBar( 59 | this.wrapper, 60 | this.classNameBar, 61 | this.height, 62 | this.colorBar, 63 | this.wrapperId, 64 | this.wrapper_color, 65 | this.rounded, 66 | this.roundedInternal 67 | ); 68 | 69 | // move the bar 70 | this.move = (percent) => { 71 | 72 | $("."+this.classNameBar).css({ 73 | width: percent+"%", 74 | transition : "width "+this.speedAnimation+"s" 75 | }); 76 | 77 | // if animation is true, reInitializate Probar. 78 | if(this.finishAnimation == true) { 79 | $("#"+this.wrapperId).css({ 80 | "height": this.height+"px" 81 | }); 82 | } 83 | 84 | // verify if is 100% 85 | setTimeout(() => { 86 | if(percent == 100 && this.finishAnimation == true) { 87 | console.log("je vais faire l'animation bro"); 88 | $("#"+this.wrapperId).css({ 89 | "height": "0px", 90 | "transition" : "all 0.3s" 91 | }); 92 | // reset bar to zero. 93 | $("."+this.classNameBar).css({ 94 | width: "0%" 95 | }); 96 | } 97 | },this.speedAnimation * 1000); 98 | } 99 | var setSpeed = (speed) => { 100 | this.speedAnimation = speed; 101 | } 102 | var setColor = (color) => { 103 | this.colorBar = color; 104 | $("."+this.classNameBar).css({ 105 | "background-color" : this.colorBar 106 | }); 107 | } 108 | var setWrapperColor = (color) => { 109 | this.wrapper_color = color; 110 | $("#"+this.wrapperId).css({ 111 | "background-color" : this.wrapper_color 112 | }); 113 | } 114 | var setFinishAnimation = (boolean) => { 115 | this.finishAnimation = boolean; 116 | } 117 | var setRounded = (topLeft = 0,topRight = 0,bottomLeft = 0,bottomRight = 0) => { 118 | 119 | this.rounded.topLeft = topLeft || 0; 120 | this.rounded.topRight = topRight || 0; 121 | this.rounded.bottomLeft = bottomLeft || 0; 122 | this.rounded.bottomRight = bottomRight || 0; 123 | 124 | $("#"+this.wrapperId).css({ 125 | "border-top-left-radius" : this.rounded.topLeft+'px', 126 | "border-top-right-radius" : this.rounded.topRight+'px', 127 | "border-bottom-left-radius" : this.rounded.bottomLeft+'px', 128 | "border-bottom-right-radius" : this.rounded.bottomRight+'px' 129 | }); 130 | } 131 | var setRoundedInternal = (topLeft = 0,topRight = 0,bottomLeft = 0,bottomRight = 0) => { 132 | 133 | this.roundedInternal.topLeft = topLeft || 0; 134 | this.roundedInternal.topRight = topRight || 0; 135 | this.roundedInternal.bottomLeft = bottomLeft || 0; 136 | this.roundedInternal.bottomRight = bottomRight || 0; 137 | 138 | $("."+this.classNameBar).css({ 139 | "border-top-left-radius" : this.roundedInternal.topLeft+'px', 140 | "border-top-right-radius" : this.roundedInternal.topRight+'px', 141 | "border-bottom-left-radius" : this.roundedInternal.bottomLeft+'px', 142 | "border-bottom-right-radius" : this.roundedInternal.bottomRight+'px' 143 | }); 144 | } 145 | var setHeight = (height = 5) => { 146 | this.height = height; 147 | $("#"+this.wrapperId).css({ 148 | "height" : this.height+'px' 149 | }); 150 | $("."+this.classNameBar).css({ 151 | "height" : this.height+'px' 152 | }); 153 | } 154 | 155 | let ProBar = { 156 | setSpeed, 157 | setHeight, 158 | setColor, 159 | setWrapperColor, 160 | setFinishAnimation, 161 | setRounded, 162 | setRoundedInternal, 163 | goto: (percent,time = null) => { 164 | if(time != null) {setSpeed(time)} 165 | this.move(percent); 166 | } 167 | }; 168 | 169 | return ProBar; 170 | } 171 | 172 | 173 | var createBar = ( element,classNameBar,height,colorBar,wrapperId,wrapper_color,rounded,roundedInternal ) => { 174 | console.log("la hauteur est de "+height); 175 | var Css = ` 176 | .${classNameBar} { 177 | width : 0px; 178 | height : ${height}px; 179 | background-color: ${colorBar}; 180 | border-radius : ${roundedInternal.topLeft}px ${roundedInternal.topRight}px ${roundedInternal.bottomLeft}px ${roundedInternal.bottomRight}px; 181 | } 182 | #${wrapperId} { 183 | width : 100%; 184 | height : ${height}px; 185 | background-color : ${wrapper_color}; 186 | overflow: hidden; 187 | border-radius : ${rounded.topLeft}px ${rounded.topRight}px ${rounded.bottomLeft}px ${rounded.bottomRight}px; 188 | } 189 | `; 190 | 191 | var htmlBar = `
for all the people who do not have the time.. 😜
67 |
258 |
259 | // options
260 | var options = {
261 | color : "#09b1ba", // color of the Progress bar.
262 | bgColor : "#efefef", // color background of the Progress bar
263 | speed : 0.3, // speed of animation. ( unit in secondes )
264 | wrapper : ".wrapper", // the wrapper who append ProBar. if use class => ".class" ,if use id => "#id"
265 | finishAnimation : false, // default "true", this option give you a animation at the end "100%".
266 | classNameBar : "proBar", // change a class to a custom class ( for Css ).
267 | wrapperId : "wrapperProBar", // change a wrapper id for a custom Css.
268 | height:10, // give a height to Probar.
269 | rounded : { // use it to round Corners of Probar.
270 | topLeft : 25,
271 | topRight : 25,
272 | bottomLeft : 25,
273 | bottomRight : 25
274 | },
275 | roundedInternal : { // use it to round Corners of Probar Internaly.
276 | topLeft : 25,
277 | topRight : 25,
278 | bottomLeft : 25,
279 | bottomRight : 25
280 | }
281 | };
282 |
283 | // instantiate
284 | var probar = new ProBar(options);
285 |
286 | // use it.
287 | probar.goto(25);
288 |
289 | // all configuration.
290 | probar.setColor("#2980b9"); // change color of ProBar.
291 | probar.setWrapperColor("#ecf0f1"); // change background color of ProBar.
292 | probar.setSpeed(3000); // change Speed to 3 secondes.
293 | probar.goto(100); // reach 100%.
294 | probar.goto(100,2000); // reach 100% in 2 secondes
295 | probar.setFinishAnimation(false); // remove a finish animation.
296 | probar.setHeight(15); // set Height to 15px.
297 | probar.setRounded(15,15,15,15); // change Border radius of Probar. (topLeft, topRight, bottomLeft, bottomRight);
298 | probar.setRoundedInternal(15,15,15,15); // change Border radius of Probar Internel. (topLeft, topRight, bottomLeft, bottomRight);
299 |
300 |
301 |