Initiate animationend prior to deletion

There is a div element with the class whatever:

<div class="whatever"></div>

Also, there is a button with the class animate:

<button class="animate">Animate</button>

Clicking on the button will initiate a 10-second animation on the div, involving bouncing, fading, dancing, rotating, or any other effect you can imagine.


Once the animation on the div is complete, an alert is triggered through an animationend event.

The code for this functionality is as follows:

$("button.animate").click(function() {
    $("div.whatever").addClass("animationThatSeemsCool").one("webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend", function() {
        alert("Whatever has finished animating!");
    });
});


However, a new button has been introduced!

This button has the class nukeit:

<button class="nukeit">Wipe whatever out!</button>

When the 'nukeit' button is clicked, it removes the 'whatever' div entirely:

$("button.nukeit").click(function() {
    $("div.whatever").remove();
});

Everything works well so far, but a problem arises:

If the user clicks on the 'animate' button and then quickly presses the 'nukeit' button while the 'whatever' div is still in the middle of its animation, the animationend event will not be triggered, and the alert will not be shown.

Is there a way to manually trigger the animationend event when the 'nukeit' button is pressed?

Update:

For a demonstration along with the attempt to manually trigger the animationend event, check out this JSFiddle link:

https://jsfiddle.net/seahorsepip/nyvfLee7/

Answer №1

Here's a suggestion to manually activate the event:

$("div.something").trigger('animationend');

Regarding your recent query, you just have to trigger one of the monitored events, such as 'animationend'.

Answer №2

The key is to simply remove the animation class that was added, it's as easy as that! ;)

Check it out here

$("button.animate").click(function() {
  $("div.whatever").addClass("animation").one("webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend", function() {
    alert("The animation has finished!");
  });
});

$("button.nukeit").click(function() {
  $("div.whatever").removeClass("animation");
  $("div.whatever").trigger('animationend');
});
.whatever {
  margin: 20px;
  width: 100px;
  height: 100px;
  background: red;
}
.animation {
  animation: rotate 10s linear 1;
}
@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="whatever"></div>
<button class="animate">Animate</button>
<button class="nukeit">Wipe it all out!</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

A guide on looping through a JSON array that has the specific structure

I am currently working with a JSON structure that looks like this: Object data: Array[1] 0:Object categories: Object cat: namecat cat2: namecate2 errors: Object err1: error1 er ...

Issue with Webpack failing to generate CSS and JavaScript files

I have encountered an issue while working with Laravel and Vue.js. Upon running NPM run dev, my Sass files are not compiling as expected and are instead being generated in the /public directory. I am unable to share the code due to non-disclosure agreeme ...

Importing a JavaScript file into an Angular 2 application

Currently, I'm in the process of developing an angular2 application using TypeScript. The Situation: Within my project, there exists a module named plugin-map.ts which is structured as follows: import { Type } from '@angular/core'; impor ...

Automated JQuery image carousel

I am currently working on a basic jQuery image slider that is set to slide automatically on a timer. My goal is to make it loop seamlessly, but it seems to be getting stuck after reaching the last slide. As I am relatively new to jQuery, I might be overloo ...

Divergent behavior of jQuery AJAX when used with a standard form versus when called in a popup within a div

Why does the jQuery AJAX work perfectly when submitting a form using the normal method, but not when submitting the same form within a popup? Below is the code for reference. Form submission script: $("#submit").click(function (e) { /* $('form&a ...

What is the best way to set up a side-opening feature in the UI design?

I am having trouble with the Amoclan shape transitioning to the next side. I need the transition to occur exactly as specified in the documentation, but for some reason it is still moving on to the next side. <html> <head> <script src=" ...

Trigger JavaScript when a specific division is loaded within a Rails 4 application

Is there a way to trigger a JavaScript function when a specific div with a certain class is loaded within my Rails 4 application? <div class="myClass"> hello world </div I am looking for a solution to execute some JavaScript code only when t ...

Issues with Javascript functionality in Internet Explorer and Google Chrome

Hello everyone, I'm experiencing an issue with a thumb image link swapping out on hover. Oddly enough, it seems to work perfectly in Firefox and Safari, but it's not showing up on IE and Chrome. As I am relatively new to Javascript, I may be over ...

Utilize setState in a ReactJS function within an if statement towards the end

Help needed! I'm facing issues trying to use the function setState within an if statement inside another function. Specifically, I'm working on creating a series of Tab components with inline styles that determine their visibility. Before returni ...

The appearance of the logo varies across various web pages on the site

Isn't it strange that my logo appears pixelated only on the home page, even though I used the same HTML and CSS code for all pages? The Html: <a href="Home.html"><img id="logo" src="../CONTENT/Images/Logo/1Sr2l8а.png" alt="logo"/></ ...

iPhone footer hides table content

Encountering issues on an iPhone while attempting to display a "timeline" consisting of a table content. The problem arises when 30 rows are present, but only 28 are visible on the screen. The remaining two rows are hidden behind the footer, requiring scro ...

Obtaining a return value from a function in Angular

After just starting to work with Angular, I am attempting to extract a value from a button displayed in the HTML using a function. `<button class="btn" id="btn-gold" (click)="value(9)" name="mybutton" value="9">` 9 I have also inclu ...

Experiencing difficulties with implementing both Bootstrap NAV TABS and Grid System on a single webpage

I'm experiencing difficulty getting both Bootstrap NAV TABS and the Grid System to work on the same page. When I try to include both, only one of them works depending on which Bootstrap reference is commented out. It seems like there may be an issue w ...

real-time visual data updates using ng2-charts animation

I am currently working on a real-time data dashboard using Angular 2 and ng2-charts. The main issue I am facing is that whenever the data associated with the chart changes, all the data points on the chart are redrawn. Since the data will be updating every ...

Adding a Nested Object to a JSON Data Structure

Recently, I had to modify the field type of a column, altering the data layout to a multiple choice field type. The original JSON fetched looked like this: d: { "results": [ { "MajorTasks": null, "DeliverablesSub ...

Three.js - interpolating keyframes

I'm experiencing some issues with object glitches when I try to disable them by setting their scale to 0. Here is the setup: In my animation, I have a cube that scales from 1 at frame 50 to 0 at frame 51. The purpose of this scaling is to disable o ...

Tips on converting fixed text to fit within a div

Facing an issue with two divs placed side by side where long text in one div overflows into the other. Check out this example for reference: http://jsfiddle.net/hjZ8F/1/ Any suggestions on how to fix this layout problem? ...

When attempting to incorporate a new middleware to manage the route in Express.js, a 404 error is encountered due to

I am looking to implement user authentication in my project where a page is returned after the user clicks the login button. To start, I utilized express-generator and wrote the code below in index.js and login.js: index.js: var express = require('e ...

What is the best way to minimize spacing within a table cell in an HTML table?

Looking for a solution to reduce spacing and padding in my html table cell? #table1 td { font-size: 200px; } <table id="table1" border="1"> <tr><td>1</td></tr> </table> https://i.sstatic.net/kVTIu.png Check it out ...

Parent passing down React state, but child component fails to update it

When a setState function is passed down from a parent component, I aim to update the state of the parent setter if the enter key is pressed. However, despite setting the state, nothing seems to happen and I am left with an empty array. Below is the snippe ...