【发布时间】:2012-01-11 04:25:00
【问题描述】:
我正在使用 -webkit-transform: translate(-958px, 0); 动画移动一个很长的图像
图像在开始水平移动后很快就被切断了,但是如果我稍微移动平板电脑屏幕,它会重新绘制屏幕并在平移时显示整个图像,也非常流畅。
但是如何在代码中模拟呢?
【问题讨论】:
标签: javascript css android-browser
我正在使用 -webkit-transform: translate(-958px, 0); 动画移动一个很长的图像
图像在开始水平移动后很快就被切断了,但是如果我稍微移动平板电脑屏幕,它会重新绘制屏幕并在平移时显示整个图像,也非常流畅。
但是如何在代码中模拟呢?
【问题讨论】:
标签: javascript css android-browser
一些建议:
回到问题:
translate3d会获得更好的性能,因为它不起作用,主要瓶颈在别处。根据我使用移动 webkit 的经验,当图像很大时(在大小或尺寸方面),您可能会遇到问题:
如果您的 UI 触发的重绘会平滑所有内容,则延迟可能是由图像加载和渲染
造成的解决方案:
animation-delay 或setTimeout 为您的动画设置合理的延迟
对于 2 & 3,代码如下:
$("<img>")
.attr({ src: " /* image url */ " })
.load(function(){
/* i. use class or animationName to set animation */
/* ii. force redraw go there if needed */
/* wrap i & ii into a setTimeout function inside this callback
if more delay is needed */
})
祝你好运。
【讨论】:
Paul Irish 的这段小代码和博客文章可能对您有所帮助:
http://paulirish.com/2011/requestanimationframe-for-smart-animating/
如果它漂浮在你的船上,它几乎是跨浏览器。
【讨论】:
如果我没有正确阅读您的问题,请原谅我。
您是否打算使用平板电脑的陀螺仪来平移大图像?这在代码中应该是可能的。
但是,据我所知,强制过渡以更快地更新是开发人员无法控制的。您能做的最好的事情就是确保平板电脑不必担心这些。
<!DOCTYPE html>
<html>
<head>
<style>
body {
padding:0px;
overflow:hidden;
}
#img {
position:absolute;
left:0px;
top:20px;
-webkit-transition:left 0.5s;
}
</style>
</head>
<body>
<img id="img" src="img.png" />
</body>
<script>
function onOrientationChange(e) {
var img = document.getElementById("img");
var maxWidth = img.clientWidth - document.body.clientWidth;
img.style.left = ((e.gamma-90) * maxWidth / 180 ) + "px";
}
window.addEventListener('deviceorientation', function(e){onOrientationChange(e)}, true);
</script>
</html>
【讨论】:
您尝试过使用 translate 3d 吗? GPU 在使用时启动。
【讨论】:
translate 似乎也在使用 GPU,因为我尝试使用 3d 但它并没有改变任何东西。