Does CSS delayed visibility transition fail to activate JavaScript transitionend event for elements' visibility changes?

My current approach involves using a clever method to delay the transition of visibility, in combination with opacity. So far, everything is working smoothly:

<body>
  <button>run</button>
  <div class="box"></div>
</body>
.box {
  visibility: hidden;
  opacity: 0;
  /* property name | duration | easing function | delay */
  transition: visibility 0s ease-in-out 3s,
              opacity 3s ease-in-out;
}

.box.show {
  visibility: visible;
  opacity: 1;
  transition-delay: 0s;
}

An issue arises when trying to monitor the transitionend event. The goal is to log both the visibility and opacity properties, but only the opacity gets logged (view pen here):

const button = document.querySelector('button');
const box = document.querySelector('.box');

// When class "show" is added, only "opacity" is logged
// When class "show" is removed, both "opacity" and "visibility" are logged
box.addEventListener('transitionend', e => {
  console.log(e.propertyName);
});

button.addEventListener('click', () => {
  box.classList.toggle('show');
});

Why do I need to listen to transitioned? This event allows me to check the number of transition properties (totalProperties). By tracking all properties that are transitioned (using a counter), I can determine when the element has completed all its transitions:

const totalProperties = window.getComputedStyle(element)['transition-property'].split(',').length;
let counter = 0;

const callback = e => {
  e.stopPropagation();

  if (++counter === totalProperties) {
    counter = 0;
    box.removeEventListener('transitionend', callback);

    console.log('All properties transitioned.');
  }
};

box.addEventListener('transitionend', callback);

Answer №1

Explore more about transitionend event here:

If both transition delay and duration are set to 0s or not declared, no transition will occur, and none of the transition events will be triggered.

transition: visibility 0s ease-in-out 3s

The transition-duration for visibility is currently 0s.

Change it to .0001s, and you'll witness the visibility transitionend event being fired.

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

Loading CSS files conditionally in Angular2's index.html

Currently, my index.html page features a dark theme: <base href="/"> <html> <head> <title>XXX</title> </head> <body> <link rel="stylesheet" type="text/css" href="assets/dark_room.css"> <my-app ...

What is the method for locating all anchor tags "In General" within the inner HTML of a div using XPath?

This query is related to a discussion on anchor tags in this thread. I am interested in identifying all anchor tags within a string. The scenario provided below is similar to the aforementioned issue, however, I aim to accomplish this using xpath and angu ...

What are the primary purposes of the site.webmanifest file?

As I navigate through an HTML Boilerplate, I come across a file named site.webmanifest. Despite my efforts to research its purpose online, I am still unclear about its significance. Could this file be essential for website development? What are the specif ...

Experiencing excessive delay in loading data into the DOM following a successful response from a service in AngularJS

I am facing a challenge in loading a large set of data into an HTML table. To begin, you need to click on a button: <a ng-click="get_product()">load data</a> This button triggers a function called get_product: $scope.get_product = func ...

It never fails to function automatically, no matter which script is being executed

By default, the script will always be executed regardless of its environment. Check out my code snippet: import { Pool } from 'pg'; import config from './../config'; const connectionString = () => { switch (process.env.NODE_EN ...

Enhance your HTML audio player with added timeline functionality

I'm currently working on incorporating an HTML audio player into my project. I've managed to get the play/pause functionality to work, but now I'm stuck on adding a timeline feature. Additionally, I'm not sure how to implement the play/ ...

Utilizing setColumns() function within Google Charts for JSON data tables

Is there a feature in the Google Charts API that functions similar to setColumns() for working with JSON data? I'm aiming to reduce PHP calls by consolidating my data into a single array and then specifying which columns Google Charts should utilize. ...

Ways to access an array's value using a variable in jade

Having trouble understanding why a variable cannot be used to call a value in an array using JS/Jade. This issue arises within a script on a .jade file. The array contains around 400 entries, and one of them is shown below: myFood[10]['Cake'] = ...

Is there a way to identify the top five elements that are most frequently occurring in the array?

I've successfully found the element that appears the most in an array, but now I'm facing a new challenge where I need to identify the top 5 elements that appear most frequently. Here is the array: [ "fuji", "acros", &q ...

Solving issues with flexboxes in Safari 5.1.7 on Windows

I am currently utilizing flex boxes for managing the layouts of my website. However, it seems that the Safari version for Windows (5.1.7) is outdated and causing all flex boxes to be dysfunctional. On MacOS systems, the same version 12.x works correctly. ...

Tips for troubleshooting a timeout issue in electron-html-to with node.js

Encountering a timeout issue while attempting to convert an html file to pdf using electron. The js app is being run through node. `{ Error: Worker Timeout, the worker process does not respond after 10000 ms at Timeout._onTimeout (C:\Users\Owner ...

Preventing the $_POST action value from being altered to match the ajax page path

My current setup involves using php, jquery, and ajax to update a particular section. The ajax call is executed successfully, but I encounter an issue where the global $_SERVER[SCRIPT_NAME] changes to match the ajax path when the requested data is returned ...

When making a Get Request from Angular, the header fails to appear for servicing

I am currently working on an angular JS application where I need to call a GET API that is OAuth 2.0 enabled, requiring a Bearer Token in the header for authentication. The method I am using to make the HTTP request is as follows: var config = { heade ...

Executing a JavaScript function within MainPage.xaml.cs codebehind file in a Windows application

I am currently working on a project developing a Windows 8.1 app using HTML5 and Javascript (Silverlight). I have encountered an issue with implementing the functionality for the hardware back button. Within the MainPage.xaml.cs Codebehind file, I need to ...

Inexperienced JavaScript user looking for help with handling XMLHttpRequest response data

It's been well over a decade since I last dabbled in JavaScript, so here I am again. My goal is to create a dynamic graph that updates in real time using data from either my backend database or my ESP32 micro-controller. While it's easy to genera ...

Issue with Prettier AutoFormatting in a project that combines TypeScript and JavaScript codebases

Recently, I've started incorporating TypeScript into an existing JavaScript project. The project is quite large, so I've decided to transition it to TypeScript gradually. Below is a snippet from my eslintrc.js file: module.exports = { parser: ...

What is the process for validating dates using JavaScript?

I'm currently working on a birthday validation form using JavaScript and I'm facing some issues. For instance, the date 40/40/2012 should be considered invalid but no alert is being triggered. Here is the JavaScript code: function validateBirth ...

Encountering difficulties with image processing on a web page

Recently, I've been experimenting with uploading an image and converting it to a grayscale version on my webpage. Oddly enough, the javascript code works perfectly when tested locally but fails to generate the grayscale image once integrated onto the ...

Refreshing a sibling component because of data changes in another sibling component within Next JS

I have a Next JS application where I am utilizing the Layout feature within the _app.tsx component. The layout includes a sidebar that is populated from an API call using a GET request. In addition, there is another API call (update request) triggered on ...

Utilizing Cordova for Windows 8.1 in Visual Studio 2015 for external image retrieval and insertion into img tag

I am encountering an issue with displaying external images in a Cordova app. While the DOM is functioning correctly, the images are failing to load. I am specifically trying to resolve this for Windows 8.1 only. In my Cordova project for JavaScript, I have ...