Animating the width of a progress bar to enhance visual appeal

I am looking to animate five Bootstrap progress bars to a specific width. Instead of defining the specific width in @keyframes five times, I am wondering if there is a way to achieve this with just one animation. Additionally, I want the animation to only occur when the element is in the viewport, and I am currently using an IntersectionObserver. However, it seems that this method is not working as expected when applied to multiple classes...

Progress bar:

const observer = new IntersectionObserver(entries => {
  entries.forEach(entry => {
    const progressbar = entry.target.querySelector('.progressbar');

    if (entry.isIntersecting) {
      progressbar.classList.add('bar1');
    return; 
    }

    
    progressbar.classList.remove('bar1');
  });
});

observer.observe(document.querySelector('.progress'));
.bar1 {
  animation: bar1 3s ease-in-out;
  animation-fill-mode:both;
}
@keyframes bar1  {
  0% { width: 0; }
  100% { width: 60%; }
}
<div class="col-sm-6">
  <h3 class="progress-title">SEO / SEM</h3>
  <div class="progress">
    <div class="progress-bar bar1">
      <div class=" progress-value">60%</div>
   </div>

Answer №1

To achieve the desired effect, consider manually adjusting the style.width property in the progressbar using JavaScript. Instead of utilizing animations, apply a transition for smoother visual changes.

.bar1 {
  transition: width 3s ease-in-out;
}

Next,

const observer = new IntersectionObserver(entries => {
  entries.forEach(entry => {
    const progressbar = entry.target.querySelector('.progressbar');

    if (entry.isIntersecting) {
      progressbar.style.width = someTargetWidth;
    return; 
    }

    progressbar.style.width = someInitialWidth;
  });
});

observer.observe(document.querySelector('.progress'));

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

Verify / Decline SweetAlert will be confirmed in both instances

When you click on "Confirm" or "Cancel", they both trigger the function "isConfirm". Interestingly, the "Cancel" button does not close the alert as expected. It appears to be clashing with the SweetAlert triggered in ...

Unable to adjust the width of a datatable using the jQuery Datatable plugin version 10.1

I am facing a challenge with multiple datatables using the jQuery plugin on my page. Some of the datatables fit perfectly within the page width, while others extend beyond the page boundaries. Despite trying various methods, including setting styles in HTM ...

Tips on dynamically looping the formcontrolname and implementing validation strategies

Looking for a way to validate multiple looping of dynamic formControlName="xxx" in select field. Check out my HTML code: <ul *ngFor="let detaillist of stressli.stresstabdetails;"> <li> <div class="form-container"> ...

Accessing a subcollection with DocumentSnapshot in Firebase using JS9

Just had a quick question. Is anyone familiar with how to achieve something similar using Firebase JavaScript v9? Essentially, I have a `userDoc` that is a DocumentSnapshot and I need to access a sub-collection with the document snapshot. Here's the c ...

Issue with Bootstrap CSS not rendering properly in Chrome and Firefox

After creating a simple "Hello World" web page and including Bootstrap CSS from a CDN, I noticed that it's only working properly in Microsoft Edge. Both Google Chrome and Mozilla Firefox are not displaying the correct output. I would greatly apprecia ...

Best method for combining objects in a tidy manner

I have multiple objects with similar structures, where some have null values for certain fields and one object (obj2) has values for those fields that are null in the others. I want to merge them and consider the values from obj2: var obj2 = { options ...

Generating a pdf Blob from an array and displaying it

I am encountering an issue with displaying my PDF file. The data is coming as a byte array, and this is how I currently have it set up: // data is initially String {0: % 1:P 2:D ...} const byteArray = _.map(data); // ["%", "P", "D", "F", "-", "1", ...

What reasons underlie the existence of various methods for importing Modules in JavaScript?

I'm confused about the distinctions when it comes to importing a JavaScript module in various ways such as: CommonJS ES5 ES6 NodeJS Typescript What is the reason for having multiple methods of importing JavaScript modules? Is the concept of a "modu ...

minimize the overlay for the full-screen menu

I have a fullscreen menu overlay with a close button, but I want to be able to close the overlay by clicking anywhere outside of the menu on the overlay itself. How can I achieve this functionality in my code? (function() { var triggerBttn = documen ...

Error in Javascript programming | tracking progress bar

After stumbling upon this code snippet at this link, I was eager to delve deeper into the world of JavaScript and jQuery. However, upon implementing these codes, I encountered a perplexing issue. The progress bar and continue buttons seem to be non-funct ...

Retrieve specific attributes in a nested eager loading sequelize query

I have a query to fetch information about shows from a database, along with details about the venue and bands involved. However, I am only interested in retrieving the names of the bands and the venue. The current code is pulling the entire record instea ...

How to pass arguments to page.evaluate (puppeteer) within pkg compiled applications

When I run my puppeteer script directly with node, everything works fine. However, once I compile the source using pkg, the page.evaluate and page.waitForFunction functions start failing with a SyntaxError: Unexpected identifier error. The specific code ...

tips for enabling communication between the server and client

As someone who is new to the world of web development, please bear with me as I ask a question out of curiosity. I am wondering if there is a method for the server to push messages to clients. For instance, imagine a client's webpage featuring a news ...

What is the best way to exclude a particular character from a text element utilizing jquery?

Looking to extract the numerical value from a div containing: <div class="balance"...>$500.48</div> The goal is to retrieve 500.48 as a number, not a string. One approach is to use alert($(".balance").text()) to verify that the content is ret ...

Get rid of the horizontal scrolling issue caused by the navbar

Take a look at this basic Bootstrap example: Check out the demo here. I'm encountering an issue with the navbar causing horizontal scrolling. Any suggestions on how to fix this? Appreciate any help, thank you! ...

What is the process of invoking a function on a specific element when it is encapsulated within an if statement in Meteor.js

Here is an example: {{#if currentUser}} <li><a class="waves-effect waves-light btn modal-trigger modal-close" href="#upload">Upload Image</a></li> {{/if}} Currently, I am implementing the following: Template.MasterLayout.onRe ...

JavaScript function that adjusts the top position when toggling

When the user clicks on a div, it expands or closes. I also want to adjust the top position of another fixed div based on the state of the original div. For instance: If the original div is expanded, I want the second div to have top:10px If the original ...

Unable to interact with the reappeared element selected using jQuery

After using developer tools in both Chrome and Firefox, I successfully created an XPath for a specific element. However, when attempting to click on the element by hovering over the returned value $x(xpath), I encountered an error. The error message read: ...

Tips for properly implementing ng-hide and ng-show across various sections of your single page application

Currently working on a Single Page Application (SPA). I've included some buttons in the side navigation with the intention of displaying different content when they are clicked. However, I encountered an issue where using ng-hide and show didn't ...

using javascript to target a specific css selector attribute

I have a CSS file with the following code: .datagrid table tbody td { color: #00496B; border-left: 1px solid #E1EEF4; font-size: 16px ;font-weight: normal; } Is there a way to use JavaScript to dynamically change the font size? The code below worked ...