jQuery/CSS/HTML || Simple Progress Bar
The following is sample code which demonstrates how to create a progress bar. Using jQuery, the progress bar is animated to simulate download progress.
REQUIRED KNOWLEDGE
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
<!-- // ============================================================================ // Author: K Perkins // Date: Jun 3, 2020 // Taken From: http://programmingnotes.org/ // File: SimpleProgressBar.html // Description: Demonstrates how to display a simple progress // bar to the page and simulate download progress. // ============================================================================ --> <!DOCTYPE html> <html> <head> <title>My Programming Notes Simple Progress Bar Demo</title> <style> .progress { width: 100%; display: inline-block; overflow: hidden; height: 28px; background-color: #ddd; border-radius: 4px; position: relative; display: flex; justify-content: center; flex-direction: column; } .progress-bar-text { position: absolute; left: 50%; font-style: italic; font-size: 1em; transform:translateX(-50%); font-family: Tahoma,helvetica,arial,sans-serif; } .progress-bar { width: 0; border-radius: 4px; height: 100%; font-size: 14px; line-height: 22px; color: #464e5b; text-align: center; background-color: #61D265; } .button { padding: 8px; background-color: #d2d2d2; height:100%; text-align:center; text-decoration:none; color:black; display: flex; justify-content: center; align-items: center; flex-direction: column; border-radius: 15px; cursor: pointer; } .button.medium { height:18px; width:80px; } .button:hover { background-color:#bdbdbd; } .inline { display:inline-block; } .main { text-align:center; margin-left:auto; margin-right:auto; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <div class="main"> <div class="progress"> <div class="progress-bar-text"></div> <div class="progress-bar" style="width: 0%;"> </div> </div> <div class="inline" style="margin: 10px;"> <div id="btnResetProgress" class="inline button medium"> Reset </div> <div id="btnStopProgress" class="inline button medium"> Stop </div> <div id="btnResumeProgress" class="inline button medium"> Resume </div> </div> <div style="margin-top: 5px;"> Click a button to demo! </div> </div> <script> $(document).ready(function() { checkButtonVisibility(); animateProgress(); $('#btnStopProgress').click(function(e) { stopProgress(); }); $('#btnResumeProgress').click(function(e) { stopProgress(); animateProgress(settings.currentWidth); }); $('#btnResetProgress').click(function(e) { stopProgress(true); animateProgress(); }); }); // Object to hold shared variables const settings = { // The refresh rate of the progress bar updateInterval: 500, // The maximum width of the progress bar maxWidth: 100, // The current width of the progress bar currentWidth: 0, // The interval ID returned from setInterval // which triggers each progress update progressInterval: null }; /** * FUNCTION: animateProgress * USE: Simulates a download progress bar. * @param initial: The initial percentage of the progress bar. * @return: N/A. */ function animateProgress(initial = 0) { settings.currentWidth = initial; settings.progressInterval = setInterval(function() { // Find the progress bar so we can adjust its width let $progressBar = $('.progress-bar'); // Variable to determine if we should stop the animation let stopInterval = false; // Make sure the progress bar element is found if (!$progressBar || $progressBar.length < 1) { // Stop progress if not found stopInterval = true; alert('Unable to find the progress bar!'); } else { // Add progress if not complete if (!progressCompleted()) { settings.currentWidth = Math.min(settings.currentWidth + getRandomInt(1, 15), settings.maxWidth); } // Check to see if progress has been completed with the recent update if (progressCompleted()) { stopInterval = true; } // Set the progress bar text with the updated percentage $('.progress-bar-text').html(settings.currentWidth + '%'); // Adjust the progress bar width to match the updated percentage $progressBar.width(settings.currentWidth + '%'); } // Stop the progress bar if requested if (stopInterval) { stopProgress(); } checkButtonVisibility(); }, settings.updateInterval); } /** * FUNCTION: stopProgress * USE: Stops progress bar animation. * @param reset: Indicates if this function is called due to resetting progress * @return: N/A. */ function stopProgress(reset = false) { if (!progressStopped()) { clearInterval(settings.progressInterval); settings.progressInterval = null; } if (!reset) { checkButtonVisibility(); } } /** * FUNCTION: checkButtonVisibility * USE: Determines if certain buttons should be visible. * @return: N/A. */ function checkButtonVisibility() { let $resumeButton = $('#btnResumeProgress'); let $stopButton = $('#btnStopProgress'); let resumeVisible = false; // Hide the resume button if: // 1. Progress has not started // 2. Progress has not stopped // 3. Progress has been completed if (!progressStarted() || !progressStopped() || progressCompleted()) { $resumeButton.hide(); resumeVisible = false; } // Show the resume button if progress is paused else if (progressStopped()) { $resumeButton.show(); resumeVisible = true; } // Show/hide the stop button if the resume button is hidden/visible if (resumeVisible) { $stopButton.hide(); } else { $stopButton.show(); } } /** * FUNCTION: progressStarted * USE: Determines if the progress animation has started. * @return: True if animation has started, false otherwise. */ function progressStarted() { return settings.currentWidth && settings.currentWidth > 0; } /** * FUNCTION: progressCompleted * USE: Determines if the progress animation has completed. * @return: True if animation has completed, false otherwise. */ function progressCompleted() { return progressStarted() && settings.currentWidth >= settings.maxWidth; } /** * FUNCTION: animateProgress * USE: Determines if the progress animation has stopped. * @return: True if animation has stopped, false otherwise. */ function progressStopped() { return !settings.progressInterval; } /** * FUNCTION: getRandomInt * USE: Gets a random integer within a given range. * @param min: min (inclusive) * @param max: max (inclusive) * @return: A random integer within a given range. */ function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; } </script> </body> </html><!-- // http://programmingnotes.org/ --> |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Was this article helpful?
👍 YesNo
Leave a Reply