Is there a way to animate a progress bar using jQuery and the percentage unit in a single instance?

Is there a way to smoothly animate my progress bar when I hover over the parent div? I have tried setting the width to "90%", but it animates multiple times if I hover over the parent div more than once. Using pixel units fixes this issue, but it is not responsive when zooming in or out. How can I make sure the animation only occurs once on hover?

My goal is to achieve the same effect as shown in the snippet below, where the progress bar animates to 90% when hovered over, but without the repeat animations when hovering repeatedly.

$(document).ready(function () {
    $("#langages").on('mouseenter', function () { 
        $("#fr").animate({ width: "90%" });
    });
});
#langages{
  height: 100px;
}

#fr {
  width: 20%;
}

.size {
  max-height: 25em;
}

.bgBlue{
    background-color: #5DD5FF;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="langages" class="bg-white col-10 text-center mx-auto mb-5 fs-5 shadow rounded size">
  <div class="col-11 mx-auto my-4 text-start">
    Français
    <div class="progress mt-1">
      <div class="progress-bar bgBlue" id="fr" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100">
        <span class="sr-only">90% Complete</span>
      </div>
    </div>
  </div>
</div>

Answer №1

To ensure the animation only runs once, you can add a class when the animation is complete and check if the element has that class on the mouseenter event. This way, the animation will only run once.

$(document).ready(function () {
    $("#langages").on('mouseenter', function () { 
      // Check if animation has already been done
      if (!$("#fr").hasClass('animationDone')) {
        // Perform animation if it has not been done yet
        $("#fr").animate({
          width: "90%"
        }, 500, function() {
          // Add class 'animationDone' to indicate animation completion
          $("#fr").addClass('animationDone');
          console.log('Animate once');
        });
      }
    });
});
#langages{
  height: 100px;
}

#fr {
  width: 20%;
}

.size {
  max-height: 25em;
}

.bgBlue{
    background-color: #5DD5FF;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="langages" class="bg-white col-10 text-center mx-auto mb-5 fs-5 shadow rounded size">
  <div class="col-11 mx-auto my-4 text-start">
    Français
    <div class="progress mt-1">
      <div class="progress-bar bgBlue" id="fr" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100">
        <span class="sr-only">90% Complete</span>
      </div>
    </div>
  </div>
</div>

Answer №2

One effective way to achieve responsiveness is by utilizing the vw unit. Additionally, you can declare variables, retrieve the screen width, and perform mathematical operations as needed.

  $("#fr").animate({ width: "90vw" });

  $(document).ready(function () {
    let userScreenWidth =String(screen.width);
    userScreenWidth = userScreenWidth.substring(0, 2) * 90;
    // console.log(userScreenWidth.substring(0, 2) * 90);
  $("#langages").on('mouseenter', function () { 
      $("#fr").animate({ width:userScreenWidth+'px' });
  });
});

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

Getting the value of cache using jQuery in an ASP.NET application

I am currently developing an application where I need to pass data from one page to another using Cache. The first page, which is an .aspx page, contains a textbox control and a button control. When the button is clicked, the following code is executed: p ...

Establishing parameters in a Socket.io chatroom

I am encountering an issue when attempting to store information in the socket room variables. The error message I receive is: UnhandledPromiseRejectionWarning: TypeError: Cannot set property 'host' of undefined This is the snippet of my code: io ...

What is the best way to navigate to a particular property within my jQuery library?

Hey everyone, I need some help with my ASP.net MVC application. I have this code in my Jquery custom library for plotting a chart and now I want to update a specific value using an ajax call. Take a look at the code snippet below: Code from Jquery custom ...

Creating web applications using Eclipse IDE and HTML5

Seeking a reliable IDE for creating HTML5 applications, I've heard Eclipse is a good option, which I am already familiar with in my coding projects. Currently using Eclipse Helios Release, wondering if I should upgrade to Eclipse Helios Service Relea ...

Ways to update a specific div upon clicking an image

Is there a way to refresh a div using an image click? I have an image with the id 'sellhide' and another div with the id 'sellChart'. Below is the code: <div class="col-xs-12 col-lg-6 col-sm-6 col-md-6 index_table_three" id="sellCha ...

Can we trust the accuracy of Function.prototype.toString for our needs?

Can I trust Function.prototype.toString to provide a valid javascript function string for user-defined functions? Do any popular javascript engines differ in how they represent function objects as strings? I came across this question, but it doesn't ...

transferring data from the interface to the processor

I'm encountering an issue with sending dropdown list values to a new page for display. When I try to load the page after selecting a building from the dropdown menu, I receive an error message. I'm unsure about what mistake I might be making. Be ...

Is there a way to execute an SQL query using ajax?

I recently came across an interesting problem involving passing an SQL query through to an ajax file. let qry = mysqli_query($this->con,"SELECT * FROM products"); To ensure proper containment, I embedded the variable within HTML using the following co ...

Designing a webpage with CSS to include an offset

I've been struggling to adjust this HTML by 300 pixels vertically and horizontally, without any luck. If you have any resources or tips on how to achieve this, I would greatly appreciate it. I already have three frames in place, and I'm attemptin ...

Angular - Ensure completion of a function call before continuing with the code execution

I'm currently working on developing a code snippet that checks for potential adverse drug reactions between two medications. Within my checkForClash() function, there is a call to getCollisionsList(), which is responsible for populating the interacti ...

Identifying the method of navigation using PerformanceNavigationTiming

Previously, I was able to determine if a user had arrived on the page by clicking the back button in the previous page using window.performance.navigation.type like this: if (window.performance.navigation.type === 2) { window.location.reload() } Howe ...

Execute a method within a nested component and trigger a function within the main component

Looking to retrieve states of a country upon change and send handleChange up to the parent for the Child component, the following code is rendered: <select className="floating-select" id = "country" type ="text" ...

Initiate a fresh start with an automatic input reset

When you perform an action in the first id = "benodigheden", I believe there should be a specific outcome that then triggers a second rule for id = "benodigheden". However, I have been unsuccessful in finding information on this topic online. It seems like ...

Ways to troubleshoot an unusual margin issue in a static column of an HTML table

I have been exploring various resources to create an HTML table with a fixed first column using CSS only. Despite my efforts, the fixed column is consistently appearing lower than the rest of the content, and I'm unsure why. Check out the code I&apos ...

What is the best way to access an element's sibling using JQuery within a function?

How can I use JQuery to set an element to match the value of its sibling? In a table with multiple fields, when clicking an Edit button, I want to copy the label value to its adjacent input field: <table style="width:90%"> <tr> <td&g ...

Converting JSON to Date in ES6/TS with Angular 2

Below is the JSON data: let JSON_RESPONSE = `{"birthDates": ["2017-01-02","2016-07-15"]}` There is a TypeScript object called Children with an array of Date objects and an ES6 constructor: class Children { birthDates: Date[] = [] constructor(values ...

Obtain meta title and description using JavaScript

I'm having trouble retrieving the meta title and description from websites entered into the URL field. When I input a URL and click fetch, nothing appears. However, when I hardcode "https://www.espn.com/" in the JS function, it pulls in the entire web ...

Why is it necessary to separate PHP from HTML?

After exploring comments on various websites, pages, and questions I've asked about the separation of PHP and HTML, I'm curious. Does it mean structuring code like this: <?php myPhpStuff(); ?> <html> <?php morePhpStuff(); ?& ...

Tips on accessing information from a JSON file

I am facing an issue while trying to extract data from a JSON object that was passed from the servlet. The keys in my JSON object are numbered, and I need to append these numbers to the names of the keys. The structure of my response is as follows: {"sha ...

Optimal approach for fetching data in a Next.js component

I am facing a challenge with my menu component that is displayed globally. What is the most effective way to populate this component with data? I am exploring the static generation feature provided by Next.js, but all of the data fetching recommendations ...