Understanding how to identify the CSS transition state with JavaScript and bypassing it

Take a look at this particular div:

<div id="someid"></div>

Here is its styling:

#someid {
  transition: background-color 10s ease;
  background-color: #FF0000;
  height: 100px;
  width: 100px;
}

#someid:hover {
  background-color: #FFFFFF;
}

I am interested in being able to determine the state (whether it is currently animating or not) of #someid using JavaScript, and potentially end the animation if possible. I attempted something similar to the solution provided in this response:

document.querySelector("#someid").style.transition = "none";

However, this did not work for an element that was already in the midst of an animation.

The goal is to be able to identify whether the element is currently animating, and if so, either wait for the animation to finish or halt it immediately, without taking any action if it is not animating.

I have already come across information regarding the transitionend event, but unfortunately it does not provide insight into whether an element is presently animating.

Answer №1

To monitor the transition event and deactivate it as needed, follow these steps:

const el = document.getElementById('transition');
let isAnimating = false;

el.addEventListener('transitionstart', function() {
  isAnimating = true;
});

el.addEventListener('transitionend', () => {
  isAnimating = false;
});

el.addEventListener('transitioncancel', () => {
  isAnimating = false;
});

function removeTransition(checkIfRunning) {
  if (checkIfRunning && !isAnimating) {
    return;
  }

  el.style.transition = "none";
}
#transition {
  width: 100px;
  height: 100px;
  background: rgba(255, 0, 0, 1);
  transition-property: transform background;
  transition-duration: 2s;
  transition-delay: 1s;
}

#transition:hover {
  transform: rotate(90deg);
  background: rgba(255, 0, 0, 0);
}
<div id="transition">Hello World</div>
<br />
<button onclick="removeTransition(false)">Remove Transition</button>
<br />
<br />
<button onclick="removeTransition(true)">Remove Transition on if running</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

Update the appearance of a webpage using a custom JavaScript function

My goal is to modify a CSS style whenever a button is clicked. In order to achieve this, I have implemented a JavaScript function that creates a new class for CSS along with several other functionalities. Here's how the JS function currently appears: ...

Deciphering the inner workings of React's DOM updates and mastering its application

I have a project where I need to implement functionality with 3 buttons, but only 2 are relevant to my current issue. One button is meant to add elements to the user's website view, while the other should remove elements. Although I am new to React, ...

Looking for assistance in designing SVG tabs?

I'm looking to create tabs that have a design similar to the one shown in this image. Initially, I attempted to achieve this using plain CSS by creating a triangle with the border trick but encountered challenges in adding the gray border. I am now co ...

Assistance needed: Creating a container with a rounded table design using CSS and HTML

Does anyone have suggestions or a demonstration of how to design a css-based bubble container in html? I am interested in creating a soft table with rounded corners. Essentially, I want the end result to resemble a table but with curved edges, and it woul ...

What is the jquery alternative to the PHP 'if IP equals' condition?

I am curious to know if the following PHP 'if' conditions can be achieved in jquery or JavaScript, with a preference for jquery. if ($_SERVER['REMOTE_ADDR'] != '123.99.55.616') { // public doingness } if ($ ...

Div expanding beyond intended size when media reaches maximum 468 pixels

When zoomimg is set to width: 145px; height: 145px; it ends up pushing the text that should be inside its parent element out of the parent. If you want to see the Zoomimg ProjectKort divs, check out the code below: .projectkort{ margin: 10px 0px 10px 0 ...

VueJS allows for seamless image uploading using either a gallery of images or input files

Is there a way to enable image selection from "Pick from gallery" feature in addition to the file upload functionality demonstrated in the example below? I would like the same behavior for selecting an image from the gallery as when uploading a file using ...

Maintaining uniform cell width in tables even when some data is missing

Hello, I have a webpage that dynamically displays various tables with different data sets. The issue I'm facing is that some columns do not have data, causing the tables to display at varying widths. This inconsistency in width is aesthetically unplea ...

Tips for expanding the IntelliSense analysis capacity of large CSS files

My CSS file is quite large at 3.5MB and Visual Studio 2022 doesn't seem to be recognizing it properly, resulting in a lack of class suggestions. Is there a method to enhance IntelliSense analysis limits and enable code completions for such sizable fi ...

Simplify a structure within an object

Currently, I'm troubleshooting a GET request that retrieves an id and the amount of holdings a user has stored in the database. This request then calls a function to fetch updated information about the item. My goal is to also incorporate the holdings ...

Utilizing AngularJS Factory to Retrieve Data from PHP Service via $http Request

I am new to JavaScript and this marks my first inquiry on StackOverflow. Therefore, any feedback or criticism is appreciated. Here you can find the AngularJS Flowchart project on GitHub. There is also another discussion on StackOverflow that explains how ...

Guide on utilizing a variable within req.body in Node.js

My goal is to extract data from a dynamically generated form, but I am unsure of how many rows will be generated. Here is the form using EJS: <form action="/attendance-data" method="post"> <% var i=0 %> <% rows.forEach(function(item){ ...

Is it possible to modify the text displayed within a select element?

Currently, I am working on enhancing a react app by making the dropdown options in a select element more informative than what is displayed as the selection. For instance: Selected: A However, when you click on the element for the dropdown it displays: ...

What steps can I take to ensure that an image does not exceed the size of its parent tag?

Is there a way to ensure that items placed in a container do not exceed the boundaries of the container? I am trying to use my logo as the home button, but when I set the image height and width to 100%, it becomes larger than the nav bar. The CSS for the n ...

Calculate the difference and sum of time values with varying signs in JavaScript

-12:00 - 5:30 => -6:30 -2:00 - 5:30 => 3:30 00:00 - 5:30 => -5:30 6:00 - 2:30 => 3:30 I am interested in subtracting time with both positive and negative indices. let myCountries = [ { countryName: "NewZealand", ...

Total of the Selected Rows

In my webpage, I have a table structured like this: <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> ...

Connect an AngularJS UI-Select to a UI-Grid filter

I am working on integrating AngularUI's ui-grid and ui-select together. This allows me to utilize ui-select functionality while filtering the ui-grid data. I have made some progress with a Plunker example that can be found here: http://plnkr.co/edit/ ...

Ways to secure and conceal JavaScript functions or files from being accessed by users

Is there a way to protect my JavaScript methods from being easily copied by users? I am looking for a solution to hide my methods in firebug as well. Any suggestions on how to achieve this? Note: I am looking for methods to hide my code from users without ...

How can I create a label with a full width in Bootstrap?

Currently, I am utilizing bootstrap in the following manner: <label> <input type="text"> </label> The issue at hand is that the label element comes hardcoded with display: inline-block;. Is there a way to remove this or make it look ...

Sending out a command does not equate to establishing Redux with an outcome

I've been grappling with this issue for the past 18 hours and I'm at a loss to figure out what's causing the problem. My redux setup is working smoothly as it dispatches actions and receives state correctly for other components. However, in ...