Move progress bars in an upward direction

Having trouble animating progress bars with jQuery? I am facing a unique issue where my progress bars are behaving in an unexpected way when using the jQuery animate() method. My goal is to animate each progress bar individually with a 0.1s delay. I need guidance on selecting the appropriate animation effect as my current one seems to be moving downwards. Can someone assist me in achieving this in the simplest manner possible?

Below is my current code snippet:

$('.vertical-bars .progress-fill span').each(function() {
    var percent = $(this).html();
    var pTop = 100 - (percent.slice(0, percent.length - 1)) + "%";

    $(this).parent().css({
        'height': percent,
        'top': pTop
    });
    $(this).parent().animate({
        height: "toggle"
    }, {
        duration: 900,
        specialEasing: {
            height: "swing"
        }
    });
});

You can view and test my progress bars setup on JSFiddle HERE.

The desired behavior should see the progress bars filling upwards, similar to the example provided HERE.

Answer №1

In order to create a dynamic effect for your bars, you have the option to animate both the height and top attributes simultaneously or ensure that they remain fixed to the horizontal axis while changing height. The latter method may require more careful consideration, whereas the former approach, although less elegant, is more straightforward.

When animating the top attribute, using toggle won't suffice since it animates from 100% to zero and back, instead of to zero. Therefore, you will need to separately animate the shrink and grow actions by utilizing the 'done' function to trigger a second animation. Here is an example similar to what you previously implemented:

$('.vertical-bars .progress-fill span').each(function () {
    var percent = $(this).html();
    var pTop = 100 - (percent.slice(0, percent.length - 1)) + "%";
    $(this).parent().css({
        'height': percent,
        'top': pTop
    });
  var self=this;
    $(this).parent().animate({
    height: 0,
    top: "100%"
  }, {
    duration: 900,
    specialEasing: {
      height: "swing",
      top: "swing"
    },
    done:function(){
        $(self).parent().animate({
        height: percent,
        top: pTop
      }, {
        duration: 900,
        specialEasing: {
          height: "swing",
          top: "swing"
        }     
      })
    }
    });  
});

Alternatively, achieving the same effect can be done through CSS animations. If time permits, I will explore this further and provide an updated solution.

Answer №2

The function height: "toggle" serves to toggle the height property, where you can set it to 0 and offset it by 100% from the top before triggering an animation that adjusts these styles.

To introduce a 100ms delay between animations, the script utilizes setTimeout while incrementing the timeout for each subsequent animation.

For a working example, check out this link: https://jsfiddle.net/kss1su0b/1/

var elm = $(this).parent();

elm.css({
    'top': '100%',
    'height': 0
});

setTimeout(function() {
    elm.animate({
        'height': percent,
        'top': pTop
    }, {
        duration: 900,
        specialEasing: {
            height: "swing"
        },
    });
}, animOffset);

animOffset += 100;

Answer №3

To reverse the animation direction effectively, it is recommended to use CSS to align the bars at the bottom:

.progress-fill {
  position: absolute;
  bottom: 0;
}

By doing so, there is no need to adjust the top using jQuery or set a delay anymore:

$('.progress-bars .progress-fill').each(function (index) {
    var percentage = $(this).find('.percentage').html();
    $(this).delay(index * 100).animate(
        {
            height: percentage
        }, {
            duration: 900,
            done: function () {
                $(this).find('span').show("normal");
            }
        }
    );
});

Check out the working example on jsfiddle here.

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

Error in AngularJS and TypeScript: Property 'module' is undefined and cannot be read

I'm attempting to incorporate TypeScript into my AngularJS 1.x application. Currently, my application utilizes Webpack as a module loader. I configured it to handle the TypeScript compilation by renaming all *.js source files to *.ts, and managed to ...

Changing the color of a selected variant in a BootstrapVue table using Vue.js

I am making use of the bootstrap-vue table row select support feature and I would like to change the color of the selected-variant prop of the b-table. Currently, the color is set to info for the selected row in the table. I wish to customize it and set th ...

Execute JavaScript using jQuery in noconflict mode, triggering the function instantly and resizing it again when necessary

I'm in the process of developing a function for a WordPress website, aligning with the recommendations outlined in the WordPress Codex http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_Wrappers This is the script I hav ...

Create an SVG timeline representation

I am attempting to create a circular timeline using SVG. I am utilizing fullpage js for this purpose. I have successfully implemented a blue line that progresses around the circle as the user scrolls. Now, I am looking to make my anchors dash array visible ...

Using Node Express.js to access variables from routes declared in separate files

Currently, I am in the process of developing a website with numerous routes. Initially, all the routes were consolidated into one file... In order to enhance clarity, I made the decision to create separate files for each route using the Router module. For ...

What is stopping me from sending data via POST - Express?

I'm encountering an issue with Express [POST request]: I have developed server-side and client-side code to send data to both the terminal and the browser.. Unfortunately, I am unable to view the result of the post function!! Please assist me, I feel ...

Missing sidebar display

Hi there! I am having an issue with my sidebar not appearing correctly when I click on the toggle button. It seems to be moving to the side, but the sidebar itself is blank or transparent. I suspect that the problem lies within my JavaScript file. As a beg ...

What is the best way to create a single title that is expressed in two distinct languages

My website features content in two local languages, but all the titles are currently only in one language. I would like to have titles in both languages alternating, with title1 displaying for 3 seconds before automatically switching to title2. This mean ...

Looking for a jQuery tool that transforms a standard table into a sortable masterpiece?

Is there a way to enhance a basic HTML table by using a jQuery plugin that allows for sorting rows based on any column? For example, something along the lines of: $('table.real_estate_values').awesomeTablePlugin(); ...

The responsiveness of Bootstrap 4 col-6 grids does not match that of col-xs-6 grids

I have come across an issue with my code snippet where I am utilizing bootstrap 4 col-6 instead of the deprecated col-xs-6. The problem arises when resizing the browser window, as the second col-6 does not adjust responsively in terms of vertical positi ...

When performing the operation number.tofixed in Typescript, it will always return a string value instead of a double datatype as expected from parseFloat

let value = 100 value.toFixed(2) -> "100.00" parseFloat(value.toFixed(2)) -> 100 I am encountering an unexpected result with the double type when input is 100.36, but not with 100.00. Framework: loopback4 ...

What could be causing my function to not loop properly without any errors being displayed?

I'm currently working through a Javascript tutorial from a book that guides me in creating a hangman game using only functions. I'm almost at the end but I've hit a roadblock... My function seems to not be looping properly, specifically, my ...

How to verify that the user is using the most up-to-date version of the application in React Native

Currently, I am focused on the application and have implemented API endpoints that return the latest version along with information on whether the update is mandatory. Within the application flow, a GET request is sent to these API endpoints to check the ...

Retrieve the initial element from each string within an array using JavaScript

Here is an example of an array: ["ds","1:0,1,2,3,4","2:0,2,3,4,5","3:0,6,7,8,9"] I am looking to extract elements that meet the following criteria: [1:0,2:0,3:0] (only the first element of each string in the array excluding the ds element) Any suggesti ...

The placement of text within a <div>

I'm new at this, so please forgive me if my question sounds naive. I've been struggling with this issue the whole day. I'm working on a student project: https://codepen.io/kremlevmax/pen/EomEyb Here is the HTML: <body> <div ...

Utilizing an undefined constant in angular programming may lead to unexpected errors

Seeking assistance in generating random first and last names based on gender for a form using Angular. I am relatively new to Angular and keep encountering this error whenever the Use of undefined constant firstName - assumed 'firstName' Here ...

The functionality of a pop-up window is not compatible with Google Maps

I recently implemented a script on my webpage that triggers a popup window every time the page is loaded. Here's how the script looks: <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>ColorBox de ...

Using Unobtrusive Ajax, learn to integrate data-ajax-confirm with sweetalert

Here is an example of a form using AJAX: <form method="post" data-ajax="true" data-ajax-method="post" data-ajax-complete="completed" data-ajax-confirm="Are you sure you want ...

I am interested in altering the font color

Hey there, I'm new to HTML and CSS and need some help changing the font color and size of my navbar. I've included my code below: HTML code: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...

Ways to access the variables of an object that initiated a function, leading to another function being called

I am facing an issue with accessing the variable b from inside the callback of setTimeout. I understand that the callback function of setTimeout only has access to the variables within its surrounding function. How can I access the this.b? Thank you! fu ...