Is there a way for me to run code once the sidebar has finished its animation and is

If we consider a scenario where there is a function as illustrated below. The intention is to execute this.sidebarVisible = false; only after all the animations related to the code mentioned in sidebarClose() are completed. Any suggestions on how this can be achieved?

ts:

  sidebarOpen() {
    const toggleButton = this.toggleButton;
    const html = document.getElementsByTagName('html')[0];

    setTimeout(function () {
      toggleButton.classList.add('toggled');
    }, 500);
    html.classList.add('nav-open');

    this.sidebarVisible = true;

  };

  sidebarClose() {
    const html = document.getElementsByTagName('html')[0];
    this.toggleButton.classList.remove('toggled');
    html.classList.remove('nav-open');

    this.sidebarVisible = false; // I want to execute this line of code after all the animations associated with the code shown above in sidebarClode() will be done. I mean three lines of code above this comment.
  };

  sidebarToggle() {

    if (this.sidebarVisible === false) {
      this.sidebarOpen();
    } else {
      this.sidebarClose();
    }
  };

html:

  <button style="right: 1vw;" class="navbar-toggler navbar-burger" type="button" data-toggle="collapse" data-target="#navbarToggler"
          aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation"
          (click)="sidebarToggle()" (transitionend)="this.printText()" >

Answer №1

Utilizing CSS for animations, as you appear to be doing, may not provide the precise control you desire since CSS lacks an animation complete callback method.

You could try approximating it using setTimeout, but this method would not guarantee accuracy. Another approach is to implement polling. For instance, if you know that the opacity of your element will reach 0 when the animation finishes, you can use setInterval like so:

const interval = setInterval(() => {
  const el = document.getElementById('myElement')
  if (window.getComputedStyle(el).getPropertyValue("opacity") === 0) {
    clearInterval(interval)
    // Add your code for when the animation completes here
  }
}, 100)

This interval will stop running once the animation is completed.

Alternatively, if you are open to using jQuery, it does offer an animation complete callback function which executes upon animation completion.

Documentation for this feature can be found here: http://api.jquery.com/animate/

Below is an example showcasing how the jQuery code could be structured:

$( "#clickme" ).click(function() {
  $( "#book" ).animate({
    opacity: 0.25,
    left: "+=50",
    height: "toggle"
  }, 5000, function() {
    console.log('Animation complete.')
  });
});

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

Accessing data from a CD-ROM or DVD through a web browser

I am currently working on developing a web application for a friend that will enable users to simply click a button to upload all the content from the CD-ROM or DVD they have inserted directly to a server. It's not feasible to rely on the standard br ...

Ensuring the validity of a signed cookie in Node using Express

I'm currently working on incorporating signed cookies in Node's express module. I've reviewed the documentation, but I'm struggling to understand how to properly verify them. My understanding is that verification must occur on the serve ...

What is the process to convert it into an Array of Objects within Angular 2?

I have been working tirelessly for the past few days to create an Array of Objects that I can store in Local Storage. The main challenge I am facing is retrieving the existing value from Local Storage. After retrieving the existing array, I need to add th ...

Ways to eliminate toggle event in Angular

I've been doing a lot of research online, but all the solutions I find are using jquery. I'm still getting the hang of Angular and Typescript. I found this and this to be unhelpful. I built a monthpicker from scratch, which has a simple and clear ...

I am interested in implementing a deep concept that involves multiple page effects using SCSS, but I do not want this effect to be applied to just one page within

Within the shared folder, there is an Auth folder with components. In auth.component.scss, we have written some CSS that acts as a parent to all components, affecting their styles. However, I do not want it to affect component 'y'. Currently, all ...

Ways to conceal one message within another

I am currently working on incorporating Facebook's comments box into my website. My goal is to have the comments that I retrieve through GET requests and display in the template remain invisible to users, but visible to search engines. As part of an ...

How can I keep the size of a Bootstrap button consistent while wrapping text inside it?

Is there a way to wrap or resize text inside a Bootstrap button without changing the button size? I have multiple buttons that need to be aligned properly. I tried using this class, and while the text wraps, the button grows in size, causing alignment iss ...

What is the process of changing the name of an object's key in JavaScript/Angular?

In my possession is an established entity, this.data = { "part": "aircraft", "subid": "wing", "information.data.keyword": "test", "fuel.keyword": "lt(6)" } My objective is to scrutinize each key and if the key includes .keyword, then eliminat ...

Strange symbols were stored in the database after saving the textarea value

After submitting a form, text entered into a text area is being saved to my database. However, in one instance, certain characters such as • are appearing in the field. For example: • Text When retrieving the data from the database using Jav ...

Eliminate the standard border line from a div element

While working on creating shapes in React using CSS, I encountered an irritating issue with a ring design that I'm attempting to create. I can't seem to get rid of the default border around the circle shape I've created. I've tried usin ...

What is the best way to retrieve a nested object array using a Signal in Angular/Typescript?

In my Angular/Typescript code, I am encountering an issue with filtering a nested object array based on the object property where value === 'event'. Despite my efforts, the code is returning the parent object array CalendarModel[] instead of the ...

When using the navbar in Tailwind CSS, the image is appearing distorted instead of aligning properly with the adjacent

I am trying to add a flag, login button, and sign up button Using class="flex", I placed an image inside it However, when I add the buttons, the image gets distorted. How can I prevent this? Expected Outcome I want to create the flag, login but ...

Received an error while using Mongoose 6 $group with Typescript stating that the property '_id' is not compatible with the index signature

Recently, I've been transitioning from JavaScript to TypeScript and also upgrading from mongoose 5.8 to version 6.1. The code snippet below used to work perfectly before: let getActionsTakenByApp = (store_url: string) => { return AppAction.aggr ...

Filtering with the Microsoft Graph Client

I am attempting to retrieve a comprehensive list of calendars owned by the user who is currently authenticated. The code below successfully retrieves calendars, but it does not specify that I want only those owned by the authenticated user. await this.gr ...

Transforming a JSON object into XML format

Currently, I am encountering an issue while attempting to convert my JSON object to XML using the xml-js library's json2xml function. When trying to implement this functionality, I keep getting an error message that states: Error: Buffer is not defin ...

Is it possible to uncheck a checkbox from a list, even if some of them are already unchecked?

I am attempting to implement a feature where checking a checkbox selects all others in the list, and unchecking one deselects all. Currently, only selecting one checkbox allows me to check all the rest, but unchecking one doesn't trigger the reverse a ...

Which option offers superior performance: using attributes through HTML or jQuery?

At times, I find myself needing to include special classes, divs, and attributes in my HTML code for jQuery scripts to function properly (the site's "no-JavaScript" version doesn't require these elements). You probably understand what I mean. I& ...

What is the best way to enclose a block of content with <div> and </div> tags using the append() function?

My goal is to add an outer div container by first appending it, then adding content, and finally appending the closing tag. However, I'm encountering an issue where the div I added at the beginning automatically closes itself, resulting in two separat ...

Is it possible to incorporate HTML content into the metadata within the head section of a Nuxt application?

Received HTML content from the backend that needs to be incorporated into the meta tag of the HTML head using nuxt. Encountered an error when attempting to display the received content View Error Outlined below is the code implementation: Snippet of Code ...

Guide to swapping images using HTML forms and JavaScript

I have been attempting to create an image swapping code using a form with two drop-down options. The options are for color choices for an item, in this case, let's call it a widget, with both exterior and interior colors. I have been brainstorming on ...