Can CSS transitions be synced up to happen simultaneously?

Here is a snippet of code showcasing CSS transitions applied to the height property. It seems that CSS transitions work asynchronously, but I am curious if there's a way to make them synchronous without using jQuery or other methods.

CSS:

.animated {
    -webkit-transform-style: preserve-3d;
    -webkit-transform: translate3d(0px,0px,0px);
    transition: height 1.5s;
    -webkit-transition: height 1.5s;
    -webkit-perspective: 1500;
    -webkit-backface-visibility: hidden;
}

HTML:

<div id='someElement' class='animated'></div>

JavaScript:

$("#someElement").css("height","200px");
//Additional JavaScript code to run after the completion of CSS animation

Answer №1

Unfortunately, there is no direct way to achieve synchronous behavior with animations. However, you can create the illusion of synchronicity by waiting for the animation to complete.

A Possible Solution

var crossBrowserEvent = 'webkitAnimationEnd oanimationend msAnimationEnd animationend';

$("#someElement").css("height","200px");

$("#someElement").one('crossBrowserEvent',function(e) {
    do_some();
    perform_magic();
    additional_actions_here();
    execute_when_animation_ends();
});

Additional Notes

Since CSS3 animations are not fully supported across all browsers, it's important to listen for multiple events. This helps avoid issues that may arise if different browsers implement events like animationEnd and webkitAnimationEnd simultaneously. Using the one event attachment method instead of on can help mitigate these conflicts.

Answer №2

Learn how to create sequential class changes using pure JS and the webkit prefix.

This code snippet showcases multiple animations with a handler function that triggers on each animation completion. By checking for a specific property name (such as 'color'), the handler function swaps classes using classList.toggle('active');, each with different transition lengths.

function handler(e){
 //Triggered when the transition ends.
 //Add your custom code here or:
 if(e.propertyName=='color'){
  e.target.classList.toggle('active');
 }
}
var div=document.getElementsByTagName('div')[0];
div.addEventListener('webkitTransitionEnd',handler,false);
div.classList.toggle('active');

Check out the DEMO for Chrome, Safari, Android, and iOS:

http://jsfiddle.net/jaqT7/

If you need additional browser support, search for prefixes like -webkit, -ms, -moz, and ensure the correct transitionend handler is used.

Explore an example of sequential animations with delays:

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

Loop through a collection of object arrays within a JavaScript context

Upon loading my HTML page, the following information is displayed: id: country url: http://data:8004/v1/entity/country name: countries description: All the countries in the world id: states url: http://data:8004/v1/entity/states name: states data descrip ...

Chrome may clip the gradient radius when setting the focal point outside of the SVG gradient

I just joined this community and I might have some questions that could be considered too basic. Recently, I've been trying to understand SVG and came across something that left me puzzled. Take a look at this: <svg xmlns="http://www.w3.org/20 ...

Handling redirection on a single-page website without javascript functionality turned on

Currently, I am in the process of creating a single-page website using html, css, javascript, and php. My aim is to use jQuery for navigating through the website - allowing the content to fade out when a user clicks on a link in the navigation menu and hav ...

Conceal the Tab Bar in Stack Navigator Excluding the tabBarVisible Setting

I discovered some solutions using outdated versions of navigation, specifically involving the "tabBarVisible" option in Tab Navigator. However, this option is no longer available, so I am seeking guidance on how to hide the Tab Bar on specific screens with ...

Adjust the Bootstrap date-time-picker to accommodate various languages, while ensuring that the selected date and time are displayed in English

As I work on my app, I've implemented localization support for multiple languages. When initializing the datetimepicker in the app, I provide the current language like this: datetimepicker({ locale: languageObj.language, format: 'DD MMM ...

Leveraging async/await within a React functional component

Just getting started with React for a new project and facing challenges incorporating async/await functionality into one of my components. I've created an asynchronous function called fetchKey to retrieve an access key from an API served via AWS API ...

Using React.js - Discover the best way to incorporate a function within a child component to unmount a different child component from the same parent, and then mount a new component in its place

Consider this scenario: import React, { Component } from 'react'; import BodyContent from './BodyContent'; import BottomOne from './BottomOne'; import BottomTwo from './BottomTwo'; class App extends Component { re ...

Authentication for file uploads in Angular 2 using Dropzone and passportjs

I am currently working on implementing authentication for an admin user using Express, Passport, and MySQL in a specific page. The authentication process works fine, but I am facing an issue with verifying whether the user is logged in while uploading file ...

Is it possible for a media query to target only 2 elements instead of all 3?

Currently, I am dealing with an older website for a client, and it appears that a media query is not correctly affecting three specific classes out of the total. These problematic classes are .free-ship, .wholesale, .chronicles The CSS styling for these c ...

Demonstrate how to pass a newline character from Ruby on Rails to JavaScript

I am working with a .js.erb file that is activated by a JavaScript function. Within this js.erb file, I have the following snippet of code: event = <%=raw @event.to_json %> $('#preview-event-body').html(event.body); The value of event.bo ...

The issue of NETWORK ERROR cannot be fixed through the use of axios

I'm attempting to communicate with a backend server that is currently offline using axios const backendClient = axios.create({ baseURL : env }); The API call is made here: export const createExpensesRecord = async (createExpenseRecordCmd) => { ...

Angular Material - Data Table Kit

Struggling with setting custom styling for a mat-table in Angular Material. I want to adjust the border radius of the table and change the spacing inside to "space-between". The design I'm aiming for is shown here: Design Any tips or suggestions woul ...

Moment.js generated an error due to an unhandled promise rejection warning

I'm trying to determine if my current timestamp is equal or greater than a certain value, but I keep encountering errors. Here's my code with the error: {...} exports.validaforgotpass = async (req, res) => { {...} const results = aw ...

Troubleshooting await and async functions in Express.js applications

How can I create an array like the one shown below? [{ "item":"item1", "subitem":[{"subitem1","subitem2"}] },{ "item":"item2", "subitem":[{"subitem3","subitem4&q ...

The variable referencing an unidentified function has not been defined

I've created an anonymous function assigned to a variable in order to reduce the use of global variables. This function contains nested functions for preloading and resizing images, as well as navigation (next and previous). However, when I try to run ...

How to organize and reuse code efficiently with Node.js, EJS, and front-end JavaScript?

It seems that I may have chosen the wrong platform by posting this question on the 'Software Engineering' exchange: Currently, my focus is on learning the MEAN stack (I have yet to dive into Angular, so for now I am using pure vanilla JS for the ...

Utilizing Angular's DomSanitizer to safely bypass security scripts

Exploring the capabilities of Angular's bypassSecurityTrust* functions has been a recent focus of mine. My objective is to have a script tag successfully execute on the current page. However, I keep encountering issues where the content gets sanitized ...

Steps to send an asynchronous AJAX request to the server-side within the JQuery validation plugin using the addMethod() function

Currently, I am in the process of developing my own framework using the JQuery validation plugin to validate CRUD forms both client-side and server-side. It is crucial that these forms are not static but rather created dynamically using "handlebar.js". Fo ...

Guide on building a "like" button using a Node.js Express application

I am in the process of developing a website using node, express, and mongodb. I am facing an issue with passing a variable value from my ejs file to the server side. Can someone help me solve this problem? Here is what I have attempted so far: Example Ht ...

How can I center-align images in WordPress?

I've been encountering an issue with my blog on Wordpress. Lately, whenever I try to add images to my articles and align them in the center while writing the post, they appear centered in the draft but end up left-aligned once the article is published ...