CSS animations和transitions再加上点JavaScript就可以实现硬件加速动画,而且其交互效果比大多数JavaScript库更高效。
So,让我们快点开始吧!小伙伴们都等不及了!
执行效果:http://cdpn.io/FIlHe
执行效果:http://cdpn.io/rmDdx
1
2
|
element.style.webkitAnimationPlayState =
"paused"
;
element.style.webkitAnimationPlayState =
"running"
;
|
执行效果:http://cdpn.io/GwBJa
1
2
3
4
5
6
7
8
9
10
11
|
function
findKeyframesRule(rule){
var
ss = document.styleSheets;
for
(
var
i = 0;i < ss.length;++i){
for
(
var
j = 0;j<ss[i].cssRules.length;++j){
if
(ss[i].cssRules[j].type == window.CSSRule.WEBKIT_KEYFRAMES_RULE && ss[i].cssRules[j].name == rule){
return
ss[i].cssRules[j];
}
}
}
return
null
;
}
|
我们一旦调用上面的函数(例如 var keyframes= findKeyframesRule(anim)),就可以通过keyframes.cssRules.length获得该对象的动画长度(这个动画中关键帧的总数量)。然后使用JavaScript的.map方法把获得到的每个关键帧值上的“%”过滤掉,这样JavaScript就可以把这些值作为数字使用。
1
2
3
4
5
6
7
8
9
10
11
12
|
// Makes an array of the current percent values
// in the animation
var
keyframeString = [];
for
(
var
i = 0; i < length; i ++)
{
keyframeString.push(keyframes[i].keyText);
}
// Removes all the % values from the array so
// the getClosest function can perform calculations
var
keys = keyframeString.map(
function
(str) {
return
str.replace(
'%'
,
''
);
});
|
1
2
3
4
5
|
totalCurrentPercent += currentPercent;
// Since it's in percent it shouldn't ever be over 100
if
(totalCurrentPercent > 100) {
totalCurrentPercent -= 100;
}
|
然后我们可以使用以下函数,在之前我们获得的关键帧数组里,找出与当前总百分比值最接近的关键帧值。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
function
getClosest(keyframe) {
// curr stands for current keyframe
var
curr = keyframe[0];
var
diff = Math.abs (totalCurrentPercent - curr);
for
(
var
val = 0, j = keyframe.length; val < j; val++) {
var
newdiff = Math.abs(totalCurrentPercent - keyframe[val]);
// If the difference between the current percent and the iterated
// keyframe is smaller, take the new difference and keyframe
if
(newdiff < diff) {
diff = newdiff;
curr = keyframe[val];
}
}
return
curr;
}
|
要获得新动画第一关键帧的位置值,我们可以使用JavaScript的.IndexOf方法。然后我们根据这个值,删除原来的关键帧定义,重新定义该关键帧。
1
2
3
|
for
(
var
i = 0, j = keyframeString.length; i < j; i ++) {
keyframes.deleteRule(keyframeString[i]);
}
|
1
2
3
4
5
6
7
8
9
10
11
|
// Prefix here as needed
keyframes.insertRule(
"0% {
-webkit-transform: translate(100px,100px) rotate("
+ (multiplier + 0) +
"deg)
translate(-100px,-100px) rotate("
+ (multiplier + 0) +
"deg);
}"
);
keyframes.insertRule(
"13% {
-webkit-transform: translate(100px,100px) rotate("
+ (multiplier + 45) +
"deg)
translate(-100px,-100px) rotate("
+ (multiplier + 45) +
"deg);
}"
);
...continued...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// Gets the browser prefix
var
browserPrefix;
navigator.sayswho= (
function
(){
var
N = navigator.appName, ua = navigator.userAgent, tem;
var
M = ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
if
(M && (tem = ua.match(/version\/([\.\d]+)/i))!=
null
) M[2] = tem[1];
M = M? [M[1], M[2]]: [N, navigator.appVersion,
'-?'
];
M = M[0];
if
(M ==
"Chrome"
) { browserPrefix =
"webkit"
; }
if
(M ==
"Firefox"
) { browserPrefix =
"moz"
; }
if
(M ==
"Safari"
) { browserPrefix =
"webkit"
; }
if
(M ==
"MSIE"
) { browserPrefix =
"ms"
; }
})();
|
如果你想进一步研究,可以访问Russell Uresti在StackOverflow上的帖子和相应的案例。
执行效果:http://cdpn.io/IdlHx
1
2
|
var
translated3D =
new
WebKitCSSMatrix(window.getComputedStyle(elem,
null
).webkitTransform);
|
但是这个过程可能有些混乱,尤其对于那些刚刚开始使用CSS animations(动画)的。