在实现各种过渡效果过程中不可避免地需要用到缓动函数,不同的缓动函数会实现不同的过渡过程,展现不同的过渡效果。可以很生硬,也可以很柔缓,或是很跳脱。以下是我整理的部分常用缓动函数:
(以下缓动函数均表示由0~1的过渡过程。描述到具体的变化数据时仅需将返回值乘上总变化量即可 : a+(b-a)*t )
再附上一个查看各种缓动函数的网站:https://easings.net/zh-cn
1:easeOutCubic 直入淡出
function easeOutQuad(x) {
return 1 - (1 - x) * (1 - x);
}
2:easeInOutQuad 淡入淡出
function easeInOutQuad(x){
return x < 0.5 ? 2 * x * x : 1 - Math.pow(-2 * x + 2, 2) / 2;
}
3:easeInQuad 淡入直出
function easeInQuad(x){
return x * x;
}
4:easeInBack 欲拒还迎
function easeInBack(x){
const c1 = 1.70158;
const c3 = c1 + 1;
return c3 * x * x * x - c1 * x * x;
}
5:easeOutBack 回光返照
function easeOutBack(x){
const c1 = 1.70158;
const c3 = c1 + 1;
return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);
}
6:easeInOutBack 来拒去留
function easeInOutBack(x){
const c1 = 1.70158;
const c2 = c1 * 1.525;
return x < 0.5
? (Math.pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2)) / 2
: (Math.pow(2 * x - 2, 2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2;
}
7:easeOutBounce 掉落弹跳
function easeOutBounce(x){
const n1 = 7.5625;
const d1 = 2.75;
if (x < 1 / d1) {
return n1 * x * x;
} else if (x < 2 / d1) {
return n1 * (x -= 1.5 / d1) * x + 0.75;
} else if (x < 2.5 / d1) {
return n1 * (x -= 2.25 / d1) * x + 0.9375;
} else {
return n1 * (x -= 2.625 / d1) * x + 0.984375;
}
}