CSS 使用转换和不透明度避免触发器布局
示例
更改某些CSS属性将触发浏览器同步计算样式和布局,当您需要以60fps进行动画处理时,这是一件坏事。
别
设置动画left并top触发布局。
#box {
left: 0;
top: 0;
transition: left 0.5s, top 0.5s;
position: absolute;
width: 50px;
height: 50px;
background-color: gray;
}
#box.active {
left: 100px;
top: 100px;
}演示花费了11.7毫秒用于渲染,花费了9.8毫秒进行绘画
做
使用transform相同的动画进行动画处理。
#box {
left: 0;
top: 0;
position: absolute;
width: 50px;
height: 50px;
background-color: gray;
transition: transform 0.5s;
transform: translate3d(0, 0, 0);
}
#box.active {
transform: translate3d(100px, 100px, 0);
}演示相同的动画,把1.3ms进行渲染,2.0ms的画。