javascript - How can I animate a progress bar's width changing? -
i'm using file reader load images html5 canvas; i'm trying build progress bar; works behaviour not smooth... mean bar starts size of 0 px , increase in jerky way; possible have smoother bar size increasing?
the code:
reader.readasdataurl(file); function updateprogress(evt) { jquery('.file-loading-bar').width(100 * (evt.loaded / evt.total) + '%'); } reader.onloadstart = function() { jquery('.file-loading-bar').width(0); } reader.onprogress = updateprogress;
here small sample put show how smooth interpolation of value of html5 <progress> bar. doesn't tie in actual file api because didn't want have keep copying giant files browser cache try , test thing. however, main point whenever have onprogress trigger, want call addprogress() function. rid of settimeout()s though because there simulate progress.
jsfiddle
html/js
<!doctype html> <html> <head> <title>smooth progress bar</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <script src="js/libs/jquery/jquery.js"></script> </head> <body> <!--our progress bar--> <progress id="mybar" value="20" max="100"></progress> <!--our button start progress simulation--> <button id="start">start</button> <!--our small script simulate progress--> <script> // progress bar element var bar = document.getelementbyid("mybar"); // add listener simulate progress $("#start").click(doprogress); // our function causes progress added function addprogress() { /* * create animation interpolate value * current value of progress bar actual new value * of progress bar. */ $({ // value @ present interpval: bar.value }).animate({ // new value after progress has happened interpval: bar.value + 10 }, { // how long interpolation should take duration: 200, // function set progress bar's value inter val step: function() { bar.value = this.interpval; } }); // check see if should end our simulation if (bar.value < bar.max) { // if aren't @ max, keep progressing settimeout(addprogress, 2000); } } // function bootstrap our progress function doprogress() { settimeout(addprogress, 500); } </script> </body> </html>
Comments
Post a Comment