Strategies for creating a dynamic progress bar using jQuery and JavaScript

I'm currently working on a project that involves increasing a percentage number while filling up the background color inside it based on the percentage value. The current setup is functional in terms of animating the background, but I need it to dynamically adjust the fill level according to the percentage. For instance, if the percentage is 50%, only half of the number should be filled with color. If it's 100%, the entire background should be colored. Below is the code snippet for reference:

var i = 0;

var perc = 0;

function buttonClick6() {

  perc += 5;
  document.getElementById('here').innerHTML = percentage(perc) + "%";
}

function buttonClick5() {
  i += 5;
  document.getElementById('demo').innerHTML = "€" + i;
}

function percentage(per) {
  return (100 / 100) * per;
}
$(document).ready(function() {
  var skillBar = $('.inner');
  var skillVal = skillBar.attr("data-progress");
  $(skillBar).animate({
    height: skillVal
  }, 2100);

});
body {
  position: absolute;
  width: 1670px;
  height: 1030px;
  font-family: arial;
  background-color: black;
}

.outer {
  width: 100%;
  height: 386px;
  overflow: hidden;
  position: relative;
  margin-top: 300px;
  text-align: center;
}

.inner {
  background: url(color3.jpg);
  bottom: 0;
  height: 0%;
  width: 100%;
  overflow: hidden;
  animation: animateMid 15s linear infinite;
  -webkit-background-clip: text;
  position: absolute;
}

.inner h2 {
  font-size: 500px;
  margin-top: -95px;
  color: rgba(225, 225, 225, .1);
}

@keyframes animateMid {
  0% {
    background-position: left 800px top 500px;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
  <div class="skill">
    <div class="outer">
      <div class="inner" data-progress="100%">
        <h2 id="demo">0</h2>
        <div></div>
      </div>
    </div>
  </div>



  <div class="perc">
    <h3 id="here">0%</h3>
  </div>

  <button class="btn" onclick="buttonClick5(),buttonClick6()">Donate 5€</button>

Answer №1

If you want to see the animation in one frame without the scrollbar, you need to adjust your CSS or HTML code. By removing the scrollbar, the entire animation will be visible within a single frame.

 var i = 0;

        var perc = 0;

        function buttonClick6() {

            perc += 5;
            document.getElementById('here').innerHTML = percentage(perc) + "%";
            document.getElementById('demo2').style.height = (125 - percentage(perc)) + "%";
        }

        function buttonClick5() {
            i += 5;
            document.getElementById('demo').innerHTML = "€" + i;
            document.getElementById('demo2').innerHTML = "€" + i
        }

        function percentage(per) {
            return (100 / 100) * per;
        }

        $(document).ready(function() {
            var skillBar = $('.inner');
            var skillVal = skillBar.attr("data-progress");
            $(skillBar).animate({
                height: skillVal
            }, 2100);

        });
body {
            position: absolute;
            width: 1670px;
            height: 1030px;
            font-family: arial;
            background-color: black;
        }
        
        .outer {
            width: 100%;
            height: 386px;
            overflow: hidden;
            position: relative;
            margin-top: 300px;
            text-align: center;
        }

h3{
 color: white;
}
        
        .inner {
    background: url(color3.jpg);
    bottom: 0;
    height: 0%;
    width: 100%;
    overflow: hidden;
    animation: animateMid 15s linear infinite;
    -webkit-background-clip: text;
    position: absolute;
}
        
        .inner h2 {
            font-size: 500px;
            margin-top: -95px;
            color: #fff;
        }
        
        @keyframes animateMid {
            0% {
                background-position: left 800px top 500px;
            }
        }

p {
    color: transparent;
    font-size: 500px;
    top: -95px;
    left: 0;
    z-index: 1;
    width: 100%;
    height: 130%;
    margin: 0;
    position: absolute;
    font-weight: bold;
    background: #2c2c2c;
    background-clip: text;
    -webkit-background-clip: text;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="skill">
        <div class="outer">
            <div class="inner" data-progress="100%" data-value="0">
<p id="demo2">0</p>
                <h2 id="demo">0</h2>
                <div></div>
            </div>
        </div>
    </div>

    <div class="perc">
        <h3 id="here">0%</h3>
    </div>

    <button class="btn" onclick="buttonClick5(),buttonClick6()">Donate 5€</button>

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

Switch off any other currently open divs

I'm currently exploring a way to automatically close other div's when I expand one. Check out my code snippet below: $( document ).ready(function() { $( ".faq-question" ).click(function() { $(this).toggleClass('open'); $(this ...

Customize the Width of DropDown List Items with Razor

I'm having trouble adjusting the width of items in a DropDownList. I want the width of the DropDown items to be different from the DropDown itself. Can anyone assist me with this? <div class="col-md-3"> @f.FormGroup().DropDownList("IdTipoAc ...

What crucial element is absent from my array.map function?

I have successfully implemented a table with v-for in my code (snippet provided). However, I am now trying to use Array.map to map one array to another. My goal is to display colors instead of numbers in the first column labeled as networkTeam.source. I at ...

Converting Selenium Webdriver Xpath value to CSS: A Step-by-Step

We are looking to transform the lengthy HTML Xpath values in the existing code into a shorter CSS value: driver.findElement(By.cssSelector(".form button")).click(); ...

Tips for implementing filters in Angular2 without using the package field in the console

I am currently experiencing an issue with a filter field in my code. The filter works fine when all the package data is present, however, some items do not have a package field. As a result, I need to filter based on the package name but I am encountering ...

The data is not appearing in the Vuetify data table

I have encountered an issue with the Vuetify data table where it is not displaying any data. Even though it shows that there is 1 row out of 1 displayed, the table body remains empty. Below is my component code: <template> <v-data-table :hea ...

The reliability of next router events can sometimes be called into question as they do not always function consistently

I've been working on creating a loading screen for my Next.js project. The issue I'm facing is that sometimes the loading message stays on the screen and doesn't go away even after the page has loaded. I suspect this may be due to the state ...

I am experiencing issues with the visibility of my background on certain mobile browsers

Apologies for repeating a similar question, but I've already tried the solutions mentioned in other posts. On my webpage, I have a table with a background image set using CSS. html { width:100% height: 100%; margin: 0px; padding: 0px; border: none; } ...

What causes an absolutely positioned element to be positioned by its sibling rather than at the top corner of the page?

Can someone explain to me why my absolutely positioned element is displaying after my child_static div? I'm under the impression that absolutely positioned elements are removed from the normal flow. So why isn't child_absolute covering the child_ ...

Using Vue to perform multiplication on data table entries

Is there a way I can use Vue.js to multiply values in a table? The values provided are for 100g of the product. For example, if I input 200g, I would like the values to double: 318kcal, 12 fat, 48 carbs, 8 protein, and 2% iron. Similarly, inputting 50g sho ...

Add a JavaScript library to the header directly from the body of a webpage, ensuring it is exclusive to a single

I am using the Google Charts JS library on a single page within my project, with external and global headers and footers. The head tags are located in a head.php file, where all required JS libraries are included. The structure of my pages is as follows: ...

Exploring new possibilities in ChartJS with the use of multiple Y

I have successfully created a line chart using Chart.js with two datasets, each having its own Y scale and axis. The code for my datasets and options is as follows: datasets: [{ fill:false, label: 'Heat', yAxisID: "y-axis-1", da ...

The error message "TypeError ".deploy" is not a function in your Hardhat environment."

Currently, I'm in the process of developing a page for minting NFTs called astro-mint. To move forward, I need to deploy my contract using hardhat. However, when I execute this command npx hardhat run scripts/deploy.js --network dexitTestnet An erro ...

What is the process for creating a unit test case for a button's onClick event with Jest?

In my attempt to unit test the onClick event of a button in a component, I encountered some challenges. Specifically, I am unsure how to properly test the onClick event when it triggers a function like Add(value). App.js function App(){ const[value,set ...

Versatile route able to handle any request thrown its way

My Node.js app is up and running smoothly using the Express.js framework. All routes in my application are set to post routes, such as: app.post('/login', function(req, res){ //perform login validation }); app.post('/AppHomepage', fun ...

Dividing Trapezoid Shapes, Cropping Background Images

I am attempting to design a website layout with trapezoid shapes similar to the attached image. Each section should have a background image that fills the background with cover or a similar effect. I successfully created the first section (dark blue) usin ...

Removing leading zeros from numeric strings in JSON data

I am facing an issue with my jQuery-based JavaScript code that is making an Ajax call to a PHP function. updatemarkers.xhr = $.post( ih.url("/AjaxSearch/map_markers/"), params).done( function(json) { <stuff> } The PHP function returns the follo ...

What is the best way to ensure Leaflet-Search functionality remains active even when a layerGroup is toggled off using L.control.layers

I am encountering challenges while using the Leaflet.Control.Search plugin by Stefano Cudini in conjunction with Leaflet's built-in function L.control.layers. When all layers are active, there are no issues with locating a specific area. However, wh ...

Is it possible to send an HTTP request from the renderer process to the main process in Electron?

Currently, I am in the midst of developing an electron video player project. My main objective is to stream video from a readable stream to an HTML video-tag. I am exploring different solutions and strategies to achieve this goal. In particular, I am cur ...

Is there a maximum size limit for the Fabric.js Path Array?

Has anyone tried plotting a line graph using Fabric.js and encountered issues with the fabric.Path? I've noticed that it stops drawing after 8 segments even though I have attempted different methods like loops and individually coding each segment. co ...