通过使用 -webkit-transform 而不是 -webkit-transition 来利用硬件加速?
我希望创建一个动画,每次单击按钮时,对象都会向右移动一定的距离。
例如,如果对象的初始位置是“left:10px”,并且每 1 个动画循环将其移动 10px,那么第一次单击后它应该位于 20px,第二次单击后它应该位于 30px,依此类推。
这是我现在的代码:
JavaScript
document.getElementById( 'move-me' ).addEventListener( 'click', function () {
var move = document.getElementById( 'move' );
move.style.left = ( move.offsetLeft + 10 ) + 'px';
}, false );
HTML
<button id="move-me">Move</button>
<div id="move"></div>
CSS
#move {
background: green;
height: 50px;
left: 0px;
position: absolute;
top: 100px;
transition: left 250ms ease-in-out;
-moz-transition: left 250ms ease-in-out;
-ms-transition: left 250ms ease-in-out;
-o-transition: left 250ms ease-in-out;
-webkit-transition: left 250ms ease-in-out;
width: 50px;
}
此代码使用 CSS3 转换,但它没有利用我的 Android 设备上的 -webkit-transform 硬件加速。我该如何解决这个问题?
I wish to create an animation where upon every click of a button, an object moves a certain amount to its right.
e.g If the initial position of the object was say "left:10px" and every 1 loop of animation moves it by say 10px, then after first click it should be at 20px, after second click it should be at 30px and so on.
Here's my code right now:
JavaScript
document.getElementById( 'move-me' ).addEventListener( 'click', function () {
var move = document.getElementById( 'move' );
move.style.left = ( move.offsetLeft + 10 ) + 'px';
}, false );
HTML
<button id="move-me">Move</button>
<div id="move"></div>
CSS
#move {
background: green;
height: 50px;
left: 0px;
position: absolute;
top: 100px;
transition: left 250ms ease-in-out;
-moz-transition: left 250ms ease-in-out;
-ms-transition: left 250ms ease-in-out;
-o-transition: left 250ms ease-in-out;
-webkit-transition: left 250ms ease-in-out;
width: 50px;
}
This code uses CSS3 transitions but it doesn't make use of the -webkit-transform hardware acceleration on my android device. How do I fix that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
选择不在
-webkit-transform
和-webkit-transition
之间,而是在left
和-webkit-transform
之间进行选择>。以下是如何使用 3D 加速:
Javascript:
http://jsfiddle.net/CjQ8H/
The choice is not between
-webkit-transform
and-webkit-transition
, it's betweenleft
and-webkit-transform
.Here is how to make use of 3d acceleration:
Javascript:
http://jsfiddle.net/CjQ8H/
您还可以使用translateX。这绝对是硬件加速。
这会将 div 向右移动 100%,但仅在硬件加速设备中效果良好且平滑。
You can also use translateX. This defintely hardware accelerates.
This would move a div to the right by 100% but nice and smooth only in hardware accelerated devices.