Troubleshooting: Issue with removeClass function when using CSS transitions

Is there an issue with my code where the removeClass is not working properly on the second click when trying to create a blink effect? Can you identify the mistake?

JavaScript:

$('div').click(function() {
    $(this).css({transition: '0s'}).addClass('qwe')
    .delay(1).queue(function() {
        $(this).css({transition: '2s'}).removeClass('qwe');
    });
});

CSS:

div{
    height: 100px;
    width: 100px;
    background: gray;
}
.qwe {
    background: green;
}

Fiddle

Answer №1

Web browsers are designed to efficiently handle consecutive style modifications by combining them together. However, when it comes to CSS transitions, they tend to be a bit too aggressive.

At the moment, there isn't a straightforward solution to this issue. The best workaround for current browsers is to trigger a restyle using

window.getComputedStyle(document).color
or a similar method before making any changes that would activate the transition (removeClass in this scenario).

For further details, take a look at Clean way to programmatically use CSS transitions from JS?.

Answer №2

Resolved the issue by implementing jQuery UI

$('div').on('click', function() {
    $(this).addClass('qwe').switchClass('qwe', '', 1000);
});

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

- Removing the img tag from an HTML document: A guide

When I use quick watch, this is the innerHtml output: <img style="width: 100%; height: 320px;" src="img/banner03.jpg"> <div class="bjqs-caption"> Automatically generated caption3 </div> However, I want the innerHTML to b ...

While using the Android Firefox browser, I noticed that the keyboard is causing my screen to shrink in size

As I work on creating a cross-platform HTML page, I have encountered an issue with text fields on Android smartphones. When I attempt to type in a text box, the keyboard pops up and causes my HTML page to shrink within the viewport. I'm curious to kn ...

Encountering a Vue syntax error following the binding of a session variable

Encountering a syntax error while attempting to bind a session variable as a prop of my Vue component. Scrutinizing my code did not reveal any mistakes, but perhaps another set of eyes may catch something. This is where I have registered my components: V ...

A new sub-schema has been created in the JSON schema specifically tailored to a certain property

I needed to create a JSON schema with 3 fields: num, key1, and key2. The fields num and key1 are required, while the field key2 is only mandatory if key1 has a value of 'abc'. Below is the schema structure I came up with: schema = { type: &ap ...

Issues with sending form data using AJAX call

Having trouble sending form values to PHP using an AJAX request. Suspecting the problem might be with data: $('#signup-form').serialize(). JS: $.ajax ({ type: "POST", url: "http://www.domain.com/includes/register.php", data: $(&apos ...

The problem I am encountering with particles.js

Having an issue with the particles.js library where the particles are displaying beneath the elements despite setting the Z-index. I have tried various solutions from CSS, Stack Overflow, Google, and YouTube but none have been effective. Here is my code s ...

What is the best way to create an adaptive <pre> tag for ASCII art without using media queries?

Currently, my username is displayed as an ASCII art banner/header and I am looking to enhance its responsiveness. Instead of relying solely on media queries, I am exploring the possibility of making it scale within a CSS grid container. This approach would ...

Postgres error in NodeJS: Cannot resolve address - ENOTFOUND

My connection string for accessing my AWS database is pg://user:pass@localhost:port/table. It works perfectly fine when connecting to localhost, but as soon as I attempt to connect to the AWS server, everything falls apart. Even a simple connection code r ...

Adjust the GTK3 tooltip color exclusively for Eclipse when it is operating on Ubuntu

I am using Eclipse Mars on Ubuntu 15.04 and looking to customize the GTK3 tooltip fg/bg color specifically for Eclipse without impacting other applications. I have come across instructions for changing this color system-wide, but I need guidance on how t ...

What methods should I employ to rectify this asymmetrical border?

Within my HTML and CSS table, I've noticed that the first column has a different height compared to the rest. It appears uneven with the columns on its right. Please execute the code snippet below. The specific block I'm referring to is labeled ...

Conceal the vertical scrollbar on a webpage effortlessly using jQuery or JavaScript when opening a popup page

When I click on the <a>click me</a> tag, the popup window loads. However, during this time, the vertical scroll bar on the background page is visible. How can I hide it while my popup is loading and make it visible again when the popup closes? ...

The use of Handlebars expressions within the {{#each}} block is crucial

I am currently working on my new portfolio site and I have a question about how to place handlebars expressions inside an #each loop. The project is an express application generated by express-generator, and I am using the express-handlebars NPM package: ...

How to align several DIVs within a container with an unspecified size

I have several blue rectangles with known dimensions (180 x 180 px) within a container of unknown size. <html> <head> <style> @import url('http://getbootstrap.com/dist/css/bootstrap.css&a ...

I would like to create a map showing the selected locations by checking them off using checkboxes

How can I draw a road map by selecting locations with checkboxes in form.php? After posting the selected locations to map.php file and assigning them to a $location array, how do I then assign these values to the waypoints array in the javascript calcRoute ...

Reorganizing JSON data using JavaScript

I've been diving into learning JavaScript and I've hit a roadblock at work with structuring JSON data. Despite spending a considerable amount of time on it, I just can't seem to crack the correct format for this JSON challenge. Here's w ...

The AJAX functionality is not triggering the necessary C# controller method

I have been facing a challenge with my AJAX implementation as I am still new to using it. The problem arises when trying to reach the C# method it should call - even after setting a breakpoint, the code is never reached and no errors are displayed. While ...

Integrate JavaScript date into the gulp-rev workflow

I have been encountering an issue while using the gulp-rev plugin to add a revision of my app/html page generated by the Yeomann webapp generator. My workflow involves zipping the app and then adding a revision, but I am having trouble replacing the hash t ...

A guide on accessing information from nested arrays in JavaScript

I am having trouble retrieving data from a JavaScript array as it keeps showing undefined. Here is the code snippet: sabhaDetailsArrayTemp.forEach(element => { let arra = []; console.log(element) //return tmp.m_category_name ; arra = this.onSa ...

JavaScript function with multiple parameters inspired by Objective-C methodology

Currently, I am developing an appcelerator module using Xcode and programming in obj-C. One of the methods I have created accepts multiple arguments, as shown below: -(void)useThis:(NSString*)this withThat:(NSString*)that{} My question is, how do I inv ...

The compatibility issue between Vue 2.6.12 and Laravel is causing difficulties for FullCalendar to function properly

Recently, I attempted to integrate the fullcalendar library version 5.9.0 into my project following the instructions provided in the documentation. <script> import '@fullcalendar/core/vdom' // resolves Vite issue import FullCalendar from ...