[Toc]
translateX與left的取舍
為了讓一個(gè)div盒子運(yùn)動(dòng)起來,可以通過修改translateX使它運(yùn)動(dòng),也可以通過定位并修改left使它水平運(yùn)動(dòng).
- timeline-frames-macbook1.png
左上方的圖片是通過改變?cè)豻op/left進(jìn)行動(dòng)畫的幀率,而右上方則是調(diào)用translate函數(shù)的幀率铣口。我們可以明顯看出左圖的每一幀都執(zhí)行了相對(duì)較長(zhǎng)時(shí)間的paint砰盐,在每一幀內(nèi)涮母,cpu都需要計(jì)算該元素的其他樣式,特別是相對(duì)需要復(fù)雜計(jì)算的盒陰影湖饱,漸變,圓角等樣式,最后都需要將這些樣式應(yīng)用到該元素內(nèi)廊谓。從這個(gè)角度看,如果對(duì)于較為老舊的移動(dòng)設(shè)備進(jìn)行相對(duì)復(fù)雜的動(dòng)畫麻削,那么效果肯定不理想蒸痹。
而通過調(diào)用 translate,會(huì)啟動(dòng)硬件加速呛哟,即在 GPU 層對(duì)該元素進(jìn)行渲染叠荠。這樣,CPU 就會(huì)相對(duì)解放出來進(jìn)行其他的計(jì)算扫责,GPU 對(duì)樣式的計(jì)算相對(duì)較快榛鼎,且保證較大的幀率。我們可以通過 2d 和 3d 的 transform 來啟用 GPU 計(jì)算。
- 簡(jiǎn)要總結(jié):因?yàn)閠ranslateX會(huì)使用GPU對(duì)元素進(jìn)行渲染,解放CPU的算力,因此相較于直接修改left,使用translateX對(duì)硬件更友好.
補(bǔ)間動(dòng)畫的代碼
var Tween = {
Linear: function (t, b, c, d) {
return c * t / d + b;
},
Quad: {
easeIn: function (t, b, c, d) {
return c * (t /= d) * t + b;
},
easeOut: function (t, b, c, d) {
return -c * (t /= d) * (t - 2) + b;
},
easeInOut: function (t, b, c, d) {
if ((t /= d / 2) < 1) return c / 2 * t * t + b;
return -c / 2 * ((--t) * (t - 2) - 1) + b;
}
},
Cubic: {
easeIn: function (t, b, c, d) {
return c * (t /= d) * t * t + b;
},
easeOut: function (t, b, c, d) {
return c * ((t = t / d - 1) * t * t + 1) + b;
},
easeInOut: function (t, b, c, d) {
if ((t /= d / 2) < 1) return c / 2 * t * t * t + b;
return c / 2 * ((t -= 2) * t * t + 2) + b;
}
},
Quart: {
easeIn: function (t, b, c, d) {
return c * (t /= d) * t * t * t + b;
},
easeOut: function (t, b, c, d) {
return -c * ((t = t / d - 1) * t * t * t - 1) + b;
},
easeInOut: function (t, b, c, d) {
if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;
return -c / 2 * ((t -= 2) * t * t * t - 2) + b;
}
},
Quint: {
easeIn: function (t, b, c, d) {
return c * (t /= d) * t * t * t * t + b;
},
easeOut: function (t, b, c, d) {
return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
},
easeInOut: function (t, b, c, d) {
if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
}
},
Sine: {
easeIn: function (t, b, c, d) {
return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;
},
easeOut: function (t, b, c, d) {
return c * Math.sin(t / d * (Math.PI / 2)) + b;
},
easeInOut: function (t, b, c, d) {
return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
}
},
Expo: {
easeIn: function (t, b, c, d) {
return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b;
},
easeOut: function (t, b, c, d) {
return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
},
easeInOut: function (t, b, c, d) {
if (t == 0) return b;
if (t == d) return b + c;
if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
}
},
Circ: {
easeIn: function (t, b, c, d) {
return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
},
easeOut: function (t, b, c, d) {
return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
},
easeInOut: function (t, b, c, d) {
if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
}
},
Elastic: {
easeIn: function (t, b, c, d, a, p) {
if (t == 0) return b;
if ((t /= d) == 1) return b + c;
if (!p) p = d * .3;
if (!a || a < Math.abs(c)) {
a = c;
var s = p / 4;
} else var s = p / (2 * Math.PI) * Math.asin(c / a);
return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
},
easeOut: function (t, b, c, d, a, p) {
if (t == 0) return b;
if ((t /= d) == 1) return b + c;
if (!p) p = d * .3;
if (!a || a < Math.abs(c)) {
a = c;
var s = p / 4;
} else var s = p / (2 * Math.PI) * Math.asin(c / a);
return (a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b);
},
easeInOut: function (t, b, c, d, a, p) {
if (t == 0) return b;
if ((t /= d / 2) == 2) return b + c;
if (!p) p = d * (.3 * 1.5);
if (!a || a < Math.abs(c)) {
a = c;
var s = p / 4;
} else var s = p / (2 * Math.PI) * Math.asin(c / a);
if (t < 1) return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b;
}
},
Back: {
easeIn: function (t, b, c, d, s) {
if (s == undefined) s = 1.70158;
return c * (t /= d) * t * ((s + 1) * t - s) + b;
},
easeOut: function (t, b, c, d, s) {
if (s == undefined) s = 1.70158;
return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
},
easeInOut: function (t, b, c, d, s) {
if (s == undefined) s = 1.70158;
if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
}
},
Bounce: {
easeIn: function (t, b, c, d) {
return c - Tween.Bounce.easeOut(d - t, 0, c, d) + b;
},
easeOut: function (t, b, c, d) {
if ((t /= d) < (1 / 2.75)) {
return c * (7.5625 * t * t) + b;
} else if (t < (2 / 2.75)) {
return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
} else if (t < (2.5 / 2.75)) {
return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
} else {
return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
}
},
easeInOut: function (t, b, c, d) {
if (t < d / 2) return Tween.Bounce.easeIn(t * 2, 0, c, d) * .5 + b;
else return Tween.Bounce.easeOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b;
}
}
}
函數(shù)說明
- Linear:線性勻速運(yùn)動(dòng)效果者娱;
- Quadratic:二次方的緩動(dòng)(t^2)抡笼;
- Cubic:三次方的緩動(dòng)(t^3);
- Quartic:四次方的緩動(dòng)(t^4)肺然;
- Quintic:五次方的緩動(dòng)(t^5)蔫缸;
- Sinusoidal:正弦曲線的緩動(dòng)(sin(t));
- Exponential:指數(shù)曲線的緩動(dòng)(2^t)际起;
- Circular:圓形曲線的緩動(dòng)(sqrt(1-t^2))拾碌;
- Elastic:指數(shù)衰減的正弦曲線緩動(dòng);
- Back:超過范圍的三次方緩動(dòng)((s+1)t^3 – st^2)街望;
- Bounce:指數(shù)衰減的反彈緩動(dòng)校翔。
參數(shù)說明
/*
* t: current time(當(dāng)前時(shí)間);
* b: beginning value(初始值)灾前;
* c: change in value(變化量)防症;
* d: duration(持續(xù)時(shí)間)。
*/
補(bǔ)間動(dòng)畫的使用
-
使一個(gè)盒子從{left:500px}處開始,5000ms內(nèi)水平線性勻速運(yùn)動(dòng)-300px
let oD1 = document.querySelector('#d1') function Linear(t, b, c, d) { return c * t / d + b } let time = 0 let timer = setInterval(() => { time += 1000 / 60 let left = Linear(time, 0, -400, 5000) oD1.style.transform = 'translateX(' + left + 'px)' if (time >= 5000) { clearInterval(timer) timer = null } }, 1000 / 60)
可以使用
requestAnimationFrame
代替setInterval
補(bǔ)間動(dòng)畫的封裝
/*
示例:
TweenAnimation(oWrapper,'translateX',0,500,5000,60,'linear')
*/
/**
* @description:
* @param {*} el 對(duì)象
* @param {*} style 屬性字符串
* @param {*} startPos 起始位置
* @param {*} endPos 終點(diǎn)位置
* @param {*} duration 持續(xù)時(shí)間
* @param {*} frame 幀數(shù)
* @param {*} type 動(dòng)畫類型
* @param {*} callback 回調(diào)函數(shù)
* @return {*} 沒有返回值
*/
function tweenAnimation(el, style, startPos, displacement, duration, frame, type, callback) {
var Tween = {
// 復(fù)制上面的補(bǔ)間動(dòng)畫在此處
}
// 初始化參數(shù)
el = (typeof el === 'object') ? el : document.querySelector(el)
let fn = null
if (/(\w+)\.?(\w+)?/g.test(type)) {
if (RegExp.$2) {
fn = Tween[RegExp.$1][RegExp.$2] ? Tween[RegExp.$1][RegExp.$2] : Tween.Linear
} else {
fn = Tween[RegExp.$1] ? Tween[RegExp.$1] : Tween.Linear
}
}
frame = typeof frame === 'number' ? frame : 60
// 設(shè)定timer的儲(chǔ)存變量
if (el[style] === undefined) {
el[style] = {}
}
// 設(shè)定interval
let dis
let time = 0
function animation() {
time += (1000 / frame)
dis = fn(time, startPos, displacement, duration)
transformCSS(el, style, dis)
if (time < duration) {
el[style].requestID = requestAnimationFrame(animation, 1000 / frame)
}
}
el[style].requestID = requestAnimationFrame(animation, 1000 / frame)
}
- 通過將
requestAnimationFrame
的請(qǐng)求ID保存在el[style].requestID
中,達(dá)到可以隨時(shí)取消補(bǔ)間動(dòng)畫的效果(通過cancaelAnimationFrame
取消)
參考博客
如何使用Tween.js各類原生動(dòng)畫運(yùn)動(dòng)緩動(dòng)算法
10.30