CSS Transition Doesn't Work Initially on Hidden Elements
When attempting to animate a hidden element's property using CSS transitions, you may encounter an issue where the element instantly displays at its final position. This is due to the relationship between the CSS Object Model (CSSOM) and the Document Object Model (DOM).
The Role of Reflow and CSSOM
CSS transitions determine their initial state based on the computed styles of an element. However, when an element has display: none, the browser ignores its computed styles since the element is effectively invisible to the CSSOM.
In your scenario, when you trigger the transition on .b, it has no computed style, as it's hidden. Therefore, the transition cannot initialize properly.
Forcing a Reflow
To solve this issue, you can force the browser to update the computed styles of the hidden element before initiating the transition. This is done by triggering a reflow.
Reflow is the process where the browser recalculates the layout and computed styles of the entire page. This can be triggered by certain DOM methods or by the browser itself when necessary, such as when the screen refreshes.
Using the requestAnimationFrame() API, you can request the browser to perform a reflow just before the next paint operation occurs. This ensures that the computed styles of your element are up-to-date when the transition begins.
Updated Code
$('button').on('click', function () { $('.b').show(); // Apply display:block synchronously requestAnimationFrame(() => { // Force a reflow document.body.offsetHeight; // Trigger the transitions $('.b').css('right', '80%'); $('.a').css('right', '80%'); }); })
The above is the detailed content of Why Doesn't My CSS Transition Work on Hidden Elements?. For more information, please follow other related articles on the PHP Chinese website!

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment
