我在 Android 上做了一些关于 CSS3 动画(使用 webkit-transition 进行转换)的研究。
CSS3 动画在 Webkit 中仍然是一个实验性功能。如果您尝试同时进行平移和缩放,您会发现 CSS 动画中存在一些小故障和/或错误(例如,请参阅http://www.youtube.com/watch?v=vZdBVzN1B8Y http://www.youtube.com/watch?v=vZdBVzN1B8Y)。换句话说,在许多版本的 Android 中,该属性-webkit-变换:矩阵(...)无法正常工作。同时获得缩放和平移的唯一正确方法是设置“-webkit-transform:缩放(...)翻译(...)” 按这个顺序。
我将在这篇文章的底部展示我的结果。
正如你在其中看到的,我已经克服了其中的大部分。
然而,Android 2.2 (Froyo) 上的某些过渡仍然存在一些“闪烁”。
现在我的问题是:有没有办法在Android 2.2上同时进行缩放和平移而不闪烁?
我也尝试过-webkit-backface-可见性:隐藏;, -webkit-视角:1000; and -webkit-transform:translate3d(..,0)但这些属性在转换过程中引入了一些故障。您可以在以下视频中看到它:http://www.youtube.com/watch?v=Aplk-m8WRUE http://www.youtube.com/watch?v=Aplk-m8WRUE转换停止后缩放将被取消。
有没有其他解决方法来抑制闪烁?
有任何想法吗?
下面是我在 Android 上 CSS3 转换的结果(1.5
<html>
<head>
<title>css3test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width">
</head>
<body>
<div id='log'></div>
<div id='box' style="background-color: blue; width:100; height:100;"></div>
<script language='JavaScript'>
function mesg(str) {
document.getElementById('log').innerHTML = str;
}
var e = document.getElementById('box');
e.style['-webkit-transition-property'] = '-webkit-transform';
e.style['-webkit-transform-origin'] = "0 0";
e.style['-webkit-transition-duration'] = '350ms';
e.style['-webkit-transition-timing-function'] = 'linear';
// These properties will suppress flicker, but spoiles scaling on Android 2.2 ...
// see http://www.youtube.com/watch?v=Aplk-m8WRUE
e.style['-webkit-backface-visibility'] = 'hidden';
e.style['-webkit-perspective'] = '1000';
var b = 0;
function doAnim() {
var trans;
switch(b){
case 0: // step 0. translate and scale at the same time
// 1) matrix
// On Android 1.5, we get no translation, but the box is only enlarged. Broken.
// On Android 2.2, the transition does not occur but the box moves instantly.
//trans = 'matrix(2,0,0,2,100,100)';
// 2) scale first, then translate
// On Androi2.2, there's some glitches.
//trans = 'scale(2,2) translate(50px,50px)';
// 3) tranlate first, then scale -- CORRECT
trans = 'translate(100px,100px) scale(2,2)';
break;
case 1: // step 1. translate
// 1) matrix
//trans = 'matrix(1,0,0,1,35,35)';
// 2) translate only -- will spoil transition --
// On Android 1.5, the transition corrupts and the box repeatedly moves in a wrong direction. Bug?
// see http://www.youtube.com/watch?v=vZdBVzN1B8Y
//trans = 'translate(35px,35px)';
// 3) tranlate first, then scale with (1,1) -- CORRECT
trans = 'translate(35px,35px) scale(1,1)';
break;
case 2: // step 2. scaling
// 1) matrix -- nope.
//trans = 'matrix(1.4,0,0,1.4,0,0)';
// 2) scale only -- will spoil transition --
//trans = 'scale(1.4,1.4)';
// 3) tranlate with (0px,0ox), then scale -- CORRECT
trans = 'translate(0px,0px) scale(1.4,1.4)';
break;
case 3: // step 3. again, translate and scale at the same time
// 1) matrix -- nope.
//trans = 'matrix(1.2,0,0,1.2,100,100)';
// 2) scale then translate -- will spoil transition --
//trans = 'scale(1.2,1.2) translate(83.33px,83.33px)';
// 3) tranlate first, then scale -- CORRECT
trans = 'translate(100px,100px) scale(1.2,1.2)';
break;
}
e.style['-webkit-transform'] = trans;
mesg('move '+b+'<br/>transform:'+trans);
b=(b+1)%4;
}
var isAndroid = (new RegExp("android","gi")).test(navigator.appVersion);
if(isAndroid) {
e.addEventListener('touchstart', doAnim, false);
} else {
e.addEventListener('mousedown', doAnim, false);
}
document.addEventListener('touchmove', function(e){ e.preventDefault(); }, false);
</script>
</body>
</html>