issue with CSS transition delay not functioning properly in display

Check out this quick demo that I put together: https://codepen.io/marekKnows_com/pen/LaRPZv

My goal is to have the red box only show up 2 seconds after the mouse hovers over the blue box, and disappear immediately when the mouse leaves.

This is the HTML code structure:

<div id="blueBox">
</div>
<div id="redBox" class="">
</div>

The CSS rules are set up like so:

#blueBox {
  background-color: blue;
  width: 200px;
  height: 50px;
}
#redBox {
  display: none;
  background-color: red;
  width: 200px;
  height: 50px;
  transition: display 0s step-end 2s;
}
#redBox.isVisible {
  transition: display 0s step-end 0s;
  display: block;
}

And here's the JavaScript snippet:

const el = document.getElementById( 'blueBox' );
const redEl = document.getElementById( 'redBox' );

el.addEventListener( 'mouseover', () => {
  redBox.classList.add( 'isVisible' );
});

el.addEventListener( 'mouseout', () => {
  redBox.classList.remove( 'isVisible' );
});

Currently, the red box seems to be appearing right away instead of waiting for the 2-second delay. Any ideas on why this might be happening?

Answer №1

To create a smooth transition effect, consider using the visibility property rather than the display property.

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

Is it possible that the images are unable to load on the page

The frontend code successfully retrieves the image links sent by the backend but encounters issues displaying them. Despite confirming that the imgUrl data is successfully fetched without any hotlink protection problems, the images are still not appearing ...

Utilizing External Libraries Added Through <script> Tags in React

My goal is to develop a Facebook Instant HTML5 application in React. Following their Quick Start guide, Facebook requires the installation of their SDK using a script tag: <script src="https://connect.facebook.net/en_US/fbinstant.6.3.js"></scrip ...

Learn how to save user input from form fields and text areas into JSON format using Angular

Is there a way to add comments using Angular when a user clicks on a button? Currently, whenever text is entered in the input field or textarea, it disappears and an empty block without any name, country, and comments is displayed. The entered text should ...

In Safari and Wamp, the PHP tag is often mistaken as a comment

Currently experimenting with PHP HTML in MAMP. When I insert the PHP tags <?php and ?> into my code, there seems to be a strange issue. Something is automatically changing them to <!--?php and ?-->, causing Safari to interpret it as comments. E ...

The function setCustomValidity() will continue to display even after the form has been successfully filled out

My form is very simple and I am trying to validate it using the required attribute: <form name="form"> <label for="Header">Title</label> <input type="text" class="span5" name="Header" ng-model="Message.header" placeholder="Title" on ...

Text Module of the Divi theme

Issue Resolved (After noticing that Divi was adding padding twice to <ul>'s in their footer and Text Module, I created a new class to remove the padding-bottom from the sub-list item.) In a Divi text module, I created an unordered list with a s ...

Enhancing an array's value with the help of a click on a designated "button"

I am working on a ReactJS project where I have an array of objects with 3 values in each array for 1 object. For example, my code looks something like this: const x = useMemo(() => [ [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5], ]); Now, I ...

Verify the channel where the bot is currently active and dispatch a message

I've been attempting to set up my bot to send a message when it joins a guild, but for some reason, it's not functioning as expected. Here is what I have tried (along with some other variations): const { PermissionsBitField } = require('dis ...

What is the method to transfer a declared object from a .ejs file to my index.js file?

I have a simple script embedded in my .ejs file. The script captures input data from the user and stores it in an object. Now, my goal is to send this object to my index.js file, where I plan to utilize the data with a node module called coap (similar to ...

Updating the chosen value in an HTML select menu dynamically using PHP

I understand that there are existing examples discussing how to dynamically set the selected tag in an HTML option tag. However, I am encountering a complex issue involving break statements and quotations while trying to accomplish this. In my code: whil ...

rely on a fresh event starting at one

Despite my limited knowledge in javascript, I managed to create a simple js calendar that I am quite proud of. However, I have been facing a challenge for the past few days. You can view my calendar here: https://jsfiddle.net/ck3nsemz/ The issue I'm ...

Issue with Firefox not entering FullScreen when using the userChrome.css file

1) Below is a screenshot of my customized Firefox interface. After tweaking my userchrome.css file to remove tabs, I noticed a large empty space at the bottom of the browser window that I cannot seem to get rid of. Despite trying various solutions, I have ...

Leveraging various libraries in React

My query revolves around a React application where I have integrated Material UI for only two components. Despite installing the entire Material UI library, will this lead to an increased bundle size for my application? Or does the build process only inc ...

Setting the root position of a div: How can it be done?

Imagine a scenario where a div element is designed to follow the mouse cursor on the screen. This functionality is achieved by manipulating the document's `mousemove` event and adjusting the div's `left` and `top` positions based on the event dat ...

Apply active class to a link element in React JS

Creating a component that displays menus from an array import React from 'react' import { Link, browserHistory,IndexLink } from 'react-router' $( document ).ready(function() { $( "ul.tabs li a" ).first().addClass("current"); ...

Create a layout using CSS that features two columns. The left column should be fluid, expanding to fill all remaining space, while the right column should be fixed

I need to ensure that the Online Users div always remains at a fixed size of 200px, while allowing the chat window next to it to resize dynamically based on available space. Essentially, I want the chat window to adjust its width as the window is resized, ...

Guide on transforming an array of objects into a fresh array

I currently have this array: const initialData = [ { day: 1, values: [ { name: 'Roger', score: 90, }, { name: 'Kevin', score: 88, }, { name: 'Steve&apo ...

Substitute mozilla_compat.js with a newer alternative in your ant script to update your JavaScript Nashorn implementation

The deprecation of Nashorn by Oracle has posed a challenge for my Apache Ant build scripts, where I heavily rely on it. Below is a brief example of my usage: try{load("nashorn:mozilla_compat.js");}catch(e){;} importClass(java.io.File); var source ...

Tips for preventing elements from wrapping in a lengthy list and using scrollbars in Firefox

When dealing with a lengthy flex list, the items (buttons) should wrap when there is not enough space available (using flex-wrap). However, an issue arises in Firefox where the items are wrapped once a scrollbar appears, even if there is ample space. In c ...

Discover the closest location using geolocation technology

My collection includes various national parks like Alcatraz Island, Death Valley, and Biscayne, each with their respective coordinates. Is there an efficient method to determine the closest park from my database based on the user's current location, ...