@pspgbhu
2018-02-05T17:15:36.000000Z
字数 556
阅读 1001
temp
通常最好的动画方式是使用 CSS Animation。下面是一个简单的示例:
@keyframes slidein {
from {
transform: scale(1);
}
to {
transform: scale(0);
}
}
.animation {
animation: 3s linear slidein;
}
.target {
width: 100px;
height: 100px;
will-change: transform;
}
// 为 .target 元素来添加动画
document.querySelector('.target').classList.add('animation');
或者,用 transform
+ transition
来完成动画也是不错的:
.target {
width: 100px;
height: 100px;
transform: scale(1);
transition: transform 1s linear;
will-change: transform;
}
.scale {
transform: scale(0);
}
// 为 .target 元素来添加动画
document.querySelector('.target').classList.add('scale');
总而言之,就是将动画的计算交给浏览器去做,而不是在 JS 中做。