Combining multiple progress bars into a single bar

Considering the title, here's an example scenario:

Imagine a setup where 80% is represented by the green bar and 90% by the darker grey bar. However, due to some coding complications, I couldn't display the 90% on the right side of the bar as intended. Instead, I had to manually edit it using Paint.

Currently, I have been utilizing the following code structure for this progress bar:

HTML:

<div id='progBar'>
    <div>
        <div>
        </div>
    </div>
</div>

JS: progress:

function (percent, current,  $element) {
    var progressBarWidth = percent * $element.width() / 100;
    var currentBarWidth = current * $element.width() / 100;
    $element.find('div').animate({ width: progressBarWidth }, 2000);
    $element.find('div div').animate({ width: currentBarWidth }, 2000);
}

CSS:

#progBar {
    width: 275px;
    height: 20px;
    border: 1px solid #BDBDBD;
    background-color: #F2F2F2;
    margin: 2px 0 0 4px;
    line-height: 13px;
    font-size: 10px;
    text-align: right;
    color: #000;
}

#progBar div {
    height: 100%;
    color: #fff;
    text-align: right;
    line-height: 25px;
    width: 0;
    background-color: #D8D8D8;
}

#progBar div div {
    height: 40%;
    border: 1px solid #BDBDBD;
    background-color: #b4d134;
    margin-top: 6px;
    line-height: 13px;
    font-size: 10px;
    text-align: left;
    color: #000;
}

The first div bar with id Progbar represents the entire progress bar. The second block refers to the primary (darker grey) progress bar, while the third block denotes the secondary green bar.

Although the code seems to be functioning well, my issue arises when attempting to showcase the percentage on the far right end of the progress bar (the 90%). It appears that the stacking of several div blocks in one line might be causing this discrepancy.

Is there an alternative approach to aligning my progress bar similar to the above depiction? I prefer not to arrange two separate bars side by side but rather stack them precisely like the image shown above.

UPDATE: Upon executing these lines of code below,

$element.find('div').animate({ width: progressBarWidth }, 2000).html(target + "%&nbsp;");
$element.find('div div').animate({ width: currentBarWidth }, 2000).html(current + "%&nbsp;");

The current percentage displays correctly on the left side, yet the target percentage on the right side fails to do so...

Answer №1

UPDATE:

I have made some changes to the code, you can view it here: http://jsfiddle.net/QtBlueWaffle/Aftyj/8/

Here is how the updated code looks now:

var easing = "linear"; //easeOutBounce

function trackProgress(percent, current, $element, duration) {

    var $ref = $('#progressBar1');
    var progressBarWidth = percent * $element.width() / 100;
    if ($element.is($ref)) {
        $element.find('div').animate({
            width: progressBarWidth
        }, duration, easing);
    } else {
        $element.find('div').animate({
            width: progressBarWidth
        }, duration, easing).html(percent + "%&nbsp;");
    }
}


// Tester 1
var currentPercentage1 = 0;
var currentPercentage2 = 0;
setInterval(function () {
    if (currentPercentage1 < 100) {
        currentPercentage1 = currentPercentage1 + 10;
        trackProgress(currentPercentage1, 0, $('#progressBar1'), 500);
        $('#overlayPercentage').html(currentPercentage1 + "%&nbsp;")
    } else {
        $('#progressBar1').find('div').css({
            width: 0
        });
        currentPercentage1 = 0;
    }
}, 1000);


// Tester 2
setInterval(function () {
    if (currentPercentage2 < 100) {
        currentPercentage2 = currentPercentage2 + 5;
        trackProgress(currentPercentage2, 0, $('#progressBar2'), 100);

    } else {
        $('#progressBar2').find('div').css({
            width: 0
        }).html("&nbsp;");
        currentPercentage2 = 0;
    }
}, 200);

Here is also the stylesheet that goes with the code:

#progressTable {
    width:400px;
}
#progressBar1 {
    height:44px;
    border: 1px solid #111;
    background-color: #292929;
}
#progressBar2 {
    position:relative;
    top:-58px;
    height:22px;
    border: 1px solid #111;
    background-color:rgba(0, 0, 0, 0);
}
.progressBar div {
    height:100%;
    color:#fff;
    line-height:22px;
    width:0;
    text-align:right;
}
#overlayPercentage {
    text-align:right;
    border:1px solid #f11;
    position:relative;
    top: -35px;
    color:#fff;
}

I hope this information is helpful.

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 use jQuery to load a page via AJAX and define a callback function on the loaded page

Currently, I am using the $('#container').load method to manage loading all the subpages of my website. However, some of these sub-pages require additional functionality such as form validation. I would like each sub-page to be self-contained, m ...

transform ajax data into an array structure

I need to convert the values retrieved from an AJAX call into an array so that I can access and format them accordingly. $(document).ready(function() { $('#documenter_nav > li a').click(function(){ var url = $(this).attr('data- ...

Why are the dots/navigation buttons not showing up on owlcarousel?

Why can't I see the navigation buttons? I've implemented jQuery and owlcarousel (). http://jsfiddle.net/bobbyrne01/s10bgckL/1/ html .. <div id="owl-demo"> <div class="item"> <img src="http://placehold.it/50x50" alt= ...

What is the best way to create a div with a smaller background color than its content?

Can someone help me with adjusting the size of a gray box in this code so that it is smaller than the text inside it? .media-body { background-color: gray; } <div class="media-body"> <h5>Lagoa Azul</h5> <span class="texto-medi ...

Issue with Bootstrap5: images not properly fitting into the carousel content

Hey, I am currently working on implementing a Bootstrap carousel on my webpage. However, I am encountering an issue with the display of images uploaded by users. When a user uploads a large image, it ends up overflowing on my webpage. How can I ensure that ...

Struggling with a Python CGI script that refuses to open a file sent to it via AJAX

After encountering multiple issues while attempting to upload a file to a server using HTML and Javascript in my previous post, I decided to take a different approach. I now have an HTML form and a Python script located in the cgi directory of my webserver ...

Displaying specific choices depending on the previous selection made

I am facing an issue in Laravel where I have two selection options, and one depends on the other. Despite multiple attempts, I haven't been able to resolve it. The database structure is as follows: companies id title channels id company_id title I ...

verify whether the image source has been altered

There is an img tag displaying the user's avatar. When they click a button to edit the image and choose a new one, the src attribute of the img changes to the new image src. How can I detect when the src changes? Below is the code for the button tha ...

Issue with conditions in window.setTimeout function

I've been grappling with a website that features a loading page lasting 5 seconds, during which users can choose to activate autoplay or not. However, even after implementing a condition in my code, the autoplay feature continues to run unexpectedly. ...

Experiencing a problem with generating nested elements using JavaScript

I'm looking to customize the default header of a div using a JavaScript function, but the final result of my code is displaying as [object HTMLDivElement]. Here's the code snippet I'm working with: function CustomizeHeader(){ va ...

A guide on converting array values to objects in AngularJS HTML

Here is my collection of objects: MyCart = { cartID: "cart101", listProducts : [ {pid:101, pname:"apple", price: 200, qty:3}, {pid:102, pname:"banana", price: 100, qty:12} ] } I have incorporated a form in ...

CSS code that fixes a textarea at the bottom of a div

I need help placing a textarea at the bottom of a fixed div: <div id=msg> <div id=content> aaaaaaaannnnnnnnnn<br> aaaaaaaannnnnnnnnn<br> aaaaaaaannnnnnnnnn<br> </div> <div id=text> <textar ...

Creating bookmarks using xhtmlrenderer in iText

Currently, I am utilizing xhtmlrenderer (commonly referred to as Flying Saucer) along with iText to transform HTML into PDF format. Could anybody provide me with guidance on generating bookmarks in this setup? A brief example would be greatly appreciated. ...

The outcome from the PHP file was JSON data, refrain from interpreting it as plain text

I've written PHP code in the file update_customer.php to save customer information. <?php date_default_timezone_set('Asia/Ho_Chi_Minh'); $response = array( 'status' => 0, 'message' => '', 'ty ...

The incorporation of styled components into the child component seems to be malfunctioning

Even after attempting to include the styled component in the child component, I noticed that the values remained unchanged. The child component displays: <a href='' className='displayCarft'>{props.craftcolor}</a> I have in ...

There seems to be a glitch in the AJAX code

I am currently working on a project that involves displaying categories and subcategories of products on a webpage. When users click on either a category or a subcategory, AJAX is used to send the selected item to a php script which then generates HTML cod ...

Inserting an Excel spreadsheet into a webpage as a table

Looking for a quick and easy way to include an Excel spreadsheet on a website? Let's say you need to display a table with 100 data points in an HTML or PHP file. ...

Activate the JavaScript loader while data is being fetched

I'm currently working on incorporating a loader into my website that should remain visible throughout the entire loading process. For example, if the load time is around 6.8 seconds (with 6.3 seconds of waiting and 0.4 seconds of downloading), I want ...

Sending PHP emails may encounter issues when there is a URL link enclosed in an anchor tag within the email body

There seems to be an issue with sending emails when including an "a href" tag in the mail body. The email sends successfully when the 'a href' and 'www' tags are removed, but this affects the display of other content as per my requirem ...

Activation of the button is required once the Recaptcha and checkbox have been successfully validated

Just diving into the world of Django and jQuery, so reaching out for some guidance (pretty much a beginner here). I'm looking to activate the Start Test button only after the recaptcha validation is confirmed on the server side and the checkbox has be ...