Trigger CSS Transition On Appended Element

本文探讨了在刚添加到DOM的元素上应用CSS过渡效果为何不按预期工作的问题。浏览器在单线程JavaScript中优化最终重绘,导致只有一帧重绘,使过渡效果无法正常播放。文章提供了两种解决方案:使用setTimeout延迟添加过渡类或在JavaScript中请求元素尺寸以触发重绘。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem

If adding css transition on a just-appended element, the transition will not work as expected. Take the below code as example.

// css
.fade-in {
	opacity: 0;
}
.fade-in.show {
	opacity: 1;
	transition: opacity 1s linear;
}
var el = document.createElement('p');
el.classList.add('fade-in');
document.body.appendChild(el);
el.classList.add('show');

This is because browsers optimizes the final repaint for all changes commited in our single-thread javascript. As we create an element and add a class, browser does not paint anything as the code doesn’t change the render tree. It update the dom tree when we append the element to document, and thus change the render tree. This is the first time the render tree updates. When we add a new class to the element, it causes the style rule tree changes and the render tree changes again. There are twice upates in the render tree, but browser will batch the first one and merge them into a final update. In conclusion, the browser calculate the final styles as you set and update in a sequatial javascript, ignoring the initial state and causing the final repaint. Therefore, the transition for a appended element doesn’t animate as there’s only one final repaint for the final styled element.

Solution

To make the transition work, we have to make the browser ‘paint’ twice (of course it will paint multiple times if transition is fired). The easiest way is to put the action the needs to be executed after the repaint in a setTimeout. Take the beginning code as example, we modify it to:

// css
.fade-in {
	opacity: 0;
}
.fade-in.show {
	opacity: 1;
	transition: opacity 1s linear;
}
var el = document.createElement('p');
el.classList.add('fade-in');
document.body.appendChild(el);
setTimeout(function() {
	el.classList.add('show');
}, 10);

Then the transition fire and animate. Here we add 20ms to the action as browser repaint a frame needs 6ms if it the fps is 60. setTimeout cause asynchorous result which you might modify it with promise if you need other callback actions after the repaint.
The second way is to cause the repaint in javascript. If we request for the demension or painted styles of the element, the browser have to repaint it and calculate it.

// css
.fade-in {
	opacity: 0;
}
.fade-in.show {
	opacity: 1;
	transition: opacity 1s linear;
}
var el = document.createElement('p');
el.classList.add('fade-in');
document.body.appendChild(el);
// cause the repaint by calculating the element's dimension
var width = el.offsetWidth;
el.classList.add('show');

It works for most of occasions, but also bring performance issues as it causes more repaints. So keep in mind that don’t use it inside actions that cause frequent repaint.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值