Create a dynamic Bootstrap progress bar that smoothly transitions from 0 to 100%

I am currently utilizing Twitter Bootstrap to construct my webpage.

Here is the snippet of HTML code I have:

<div class="btn-group">
    <button type="button" class="btn btn-success">Connect</button>
    <button type="button" class="btn btn-success dropdown-data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        <span class="caret"></span>
        <span class="sr-only">Toggle Dropdown</span>
    </button>
    <ul class="dropdown-menu">
        <li><a href="#"> Connect 1</a></li>    
        <li role="separator" class="divider"></li>
        <li><a href="#"> Connect 2</a></li>
    </ul>
</div>

<div class="progress">
    <div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0%;"></div>
</div>

<div class="alert alert-success" role="alert">Well done! You successfully connected. <a href="#step2" class="alert-link">Next</a></div>

My goal is to create an animation for the progress bar, transitioning from 0% to 100% when the user clicks either the connect button or the dropdown button (either one). Once the progress bar reaches 100%, a previously hidden alert message should be displayed.

Answer №1

Here is a simple way to create an animated progress bar:

var $progressBar = $('.progress-bar').css('width', '80%');

This code snippet will smoothly animate the progress bar from its current value to 80%.


Check out this demonstration

var $progress = $('.progress');
var $progressBar = $('.progress-bar');
var $alert = $('.alert');

setTimeout(function() {
    $progressBar.css('width', '10%');
    setTimeout(function() {
        $progressBar.css('width', '30%');
        setTimeout(function() {
            $progressBar.css('width', '100%');
            setTimeout(function() {
                $progress.css('display', 'none');
                $alert.css('display', 'block');
            }, 500); // WAIT 5 milliseconds
        }, 2000); // WAIT 2 seconds
    }, 1000); // WAIT 1 second
}, 1000); // WAIT 1 second
.progress, .alert {
    margin: 15px;
}

.alert {
    display: none;
}
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.1.min.js"></script>
<div class="progress">
    <div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0%;"></div>
</div>

<div class="alert alert-success" role="alert">Great job! You have successfully connected. <a href="#step2" class="alert-link">Next</a></div>

(view this Fiddle)

Answer №2

In the latest version of bootstrap, v4, the progress bar animation has been removed as a default feature. To incorporate animation, you can include the transition-duration property in the progress bar element to control the speed of transition from 0 to the new width.

<div class="progress-bar progress-bar-animated" role="progressbar" style="transition-duration:300ms;"></div>

Answer №3

Give this code a try:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
<title>Bootstrap 3 Progress Bar Example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<style type="text/css">
.bs-sample{
    margin: 20px;
}
</style>
</head> 
<body>
<div class="bs-sample">
    <h2>Progress Tracker</h2>
    <div class="progress">
        <div class="progress-bar" id="bar">
            <span class="sr-only">35% Done</span>
        </div>
    </div>
    <script type="text/javascript">
        var n = 0;
        var progressIndicator = $("#bar");
        function countUp(){
            if(n < 100){
                n = n + 1;
                progressIndicator.css("width", n + "%");
            }
            // Set a brief waiting period before repeating the script
            setTimeout("countUp()", 500);
        }
        countUp();
    </script>
</div>
</body>
</html> 

Referenced from:

Answer №4

By default, the bootstrap progress bar comes with built-in animation. Simply updating the value of the progress will trigger the animation of the bar. All you have to do is assign the new value to the progress:

let progressBar = $(".progress-bar");
progressBar.attr("aria-valuenow", updatedValue);
progressBar.css("width", updatedValue + "%");

Answer №5

While this information may be dated, there is a helpful code snippet below that can assist you in animating multiple progress bars on a single page. Simply follow the instructions to add more progress bars as needed.

//Custom JavaScript for animating progress bars START

function animateProgress(num, id) {
  /* num --> percentage of width
   * id --> id of progress bar
   */

  var progressBar = $(id);
  for (var i = 0; i < num; i++) {
    progressBar.css("width", i + "%");
  }
}

//First progress bar set to 65%
animateProgress(65, "#barOne");

//Second progress bar set to 100%
animateProgress(100, "#barTwo");
//Custom JavaScript for animating progress bars END
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="progress" id="setProg">
  <div class="progress-bar align-middle" id="barOne">
    <p>Some Text Here</p>
  </div>
</div>
<div class="m-3"></div>
<div class="progress" id="setProg">
  <div class="progress-bar" id="barTwo">
    <span class="sr-only">60% Complete</span>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to generate a dynamic submenu and trigger events using the append method?

As I attempted to add a submenu within another submenu on a navigation bar, my goal was to have the submenu "reports management" appear when the Report option is clicked. Below is the code snippet I used. HTML <li class="drop-down"&g ...

What is the best way to retrieve a specific value from a JSON file using a numerical identifier?

Imagine a scenario where there is a JSON file structured like this: { "data": [ { "id": "1", "name": "name1" }, { "id": "2", "name": "name2" } ] } If you know the ...

Looking for a dynamic submenu that remains active even after refreshing the page? Check out the jQuery

I encountered an issue with a menu I created using jQuery. When clicking on a submenu, the site refreshes and then the menu does not remain in the active state. I want it to stay active showing its menu and submenu when clicked. I have also created a fid ...

Angular receives error when making $http calls that return a 200 response

I am facing an issue with a code snippet used in an Angular service:- var formData = function (data) { var fd = new FormData(); angular.forEach(data, function (value, key) { fd.append(key, value); }); return fd; } var updateUserPic = fu ...

Make sure the text remains separate while on the same line as the div element

I am currently using CSS to make part of my text bold, but I only want a specific portion of the line to be bold. How can I achieve this effect without making the entire line bold? Check out my code below: HTML <div> <div id="titleLabels"> ...

Adding data to a defaultContent JSON column in JQuery DataTable using JavaScript

I am working with a dynamic jQuery data table. The final column in my table allows users to delete rows, but I am having trouble passing the itemId value to the specified function within the button's onClick attribute. Here is the code I have tried s ...

Tips for creating a consistent format based on test cases

var years = Math.floor(seconds / (3600*24*365)) seconds -= years*3600*24*365 var days = Math.floor(seconds / (3600*24)) seconds -= days*3600*24 var hrs = Math.floor(seconds / 3600) seconds -= hrs*3600 var minutes = Math.floor(seconds / 60) ...

The jQuery UI datepicker fails to display if the element has previously been hidden

In the process of creating a wizard, I have designed each step to remain hidden until the user reaches that specific stage. The second step of the form is dynamically generated based on the input provided in the first step. Here is a glimpse of how it all ...

Exploring the Live Search Functionality on an HTML Webpage

I am attempting to create a live search on dive elements within an HTML document. var val; $(document).ready(function() { $('#search').keyup(function() { var value = document.getElementById('search').value; val = $.trim(val ...

What is the best way to eliminate excess spacing between HTML td and table elements in Outlook?

Here's the HTML email code snippet. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Co ...

Incorporate a JavaScript file into a JSON document

Can I import a js file into a json file? I have a js file called config.js module.exports = {id : 'test'} I would like to utilize the id key in my json file, config.json const {id } = require('./config.js') {user_id : id} ...

Storing bytes in Java until saving the Ajax file upload

As a Java developer utilizing Tapestry5, I have integrated a jQuery plugin to facilitate file uploads via ajax. My challenge lies in determining the most effective way to temporarily store the attachments until the page is saved. The current process involv ...

Convert a tuple into a JSON string

Looking to serialize a tuple into JSON: List<Tuple<string, string, string, string, string, string>> iCalEvents = new List<Tuple<string, string, string, string, string, string>>(); When I use the following code to output the value: ...

Modify the contents of an array within a string using JavaScript

let message = "hello https://ggogle.com parul https://yahoo.com and http://web.com"; let url = ["https://ggogle.com", "https://yahoo.com", "http://web.com"]; I'm trying to replace all URLs in the 'message' array with "***" from the 'ur ...

Vue component with a variable number of rows, each containing a variable number of input fields

I am currently working on creating a form that can have a variable number of steps. Users should be able to add an additional step by clicking a button. Each step will contain some input fields and buttons to dynamically create more input fields. For inst ...

Remove the list by conducting a comparison analysis

<html> <head> <title> Manipulating List Items Using JavaScript </title> <script type="text/javascript"> function appendNewNode(){ if(!document.getElementById) return; var newlisttext = document.changeform.newlist.val ...

Create a list of items that are enclosed within a parent element

My menu is generated from a php query and the output is as follows: #ultra-menu { width: 92%; background-color: rgba(255, 255, 255, 0.90); position: absolute; left: 0px; right: 0px; margin: auto; border-radius: 35px; max-height: 300px; ...

Link not functioning correctly on mobile-responsive CSS

Having some trouble with hyperlinks over a background image. The hyperlinks work fine normally, but in mobile view, nothing happens when clicking on them. Below is the code for the hyperlinks. You can also visit the site and see that the "Post a Project" ...

Joining arguments beyond argv[2] in a Node.js application

Recently, I received a suggestion from Mykola Borysyuk to use node-optimist. However, the author mentioned that it's deprecated and recommended using Minimist. Even after going through the information, I have a basic understanding. Can someone provid ...

Ensure that a child div is displayed above the parent div that is absolutely positioned

Is it possible to have a child div within an absolutely positioned parent div appear above its parent, aligned in such a way that the top of the child div aligns with the bottom of the parent div, while also being displayed above neighboring parent divs on ...