通过使用 -webkit-transform 而不是 -webkit-transition 来利用硬件加速?

发布于 2024-12-25 04:24:38 字数 1018 浏览 0 评论 0原文

我希望创建一个动画,每次单击按钮时,对象都会向右移动一定的距离。

例如,如果对象的初始位置是“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缱倦旧时光 2025-01-01 04:24:38

选择不在 -webkit-transform-webkit-transition 之间,而是在 left-webkit-transform 之间进行选择>。

以下是如何使用 3D 加速:

#move {
    background: green;
    height: 50px;
    left: 0px;
    position: absolute;
    top: 100px;
    -moz-transition:    -moz-transform 250ms ease-in-out;
    -ms-transition:     -ms-transform 250ms ease-in-out;
    -o-transition:      -o-transform 250ms ease-in-out;
    -webkit-transition: -webkit-transform 250ms ease-in-out;
    transition:             transform 250ms ease-in-out;
    width: 50px;
}

Javascript:

document.getElementById( 'move' ).addEventListener( 'click', function() {
    var move = document.getElementById( 'move' );
    var transform = "translate3d("+ (move.offsetLeft+200) + "px, 0, 0)";
    move.style.webkitTransform = transform;
    move.style.mozTransform = transform;
    move.style.msTransform = transform;
    move.style.oTransform = transform;
    move.style.transform = transform;

}, false );

http://jsfiddle.net/CjQ8H/

The choice is not between -webkit-transform and -webkit-transition, it's between left and -webkit-transform.

Here is how to make use of 3d acceleration:

#move {
    background: green;
    height: 50px;
    left: 0px;
    position: absolute;
    top: 100px;
    -moz-transition:    -moz-transform 250ms ease-in-out;
    -ms-transition:     -ms-transform 250ms ease-in-out;
    -o-transition:      -o-transform 250ms ease-in-out;
    -webkit-transition: -webkit-transform 250ms ease-in-out;
    transition:             transform 250ms ease-in-out;
    width: 50px;
}

Javascript:

document.getElementById( 'move' ).addEventListener( 'click', function() {
    var move = document.getElementById( 'move' );
    var transform = "translate3d("+ (move.offsetLeft+200) + "px, 0, 0)";
    move.style.webkitTransform = transform;
    move.style.mozTransform = transform;
    move.style.msTransform = transform;
    move.style.oTransform = transform;
    move.style.transform = transform;

}, false );

http://jsfiddle.net/CjQ8H/

勿挽旧人 2025-01-01 04:24:38

您还可以使用translateX。这绝对是硬件加速。

targetDiv.style.webkitTransition = "0ms"

targetDiv.style.webkitTransform = "translateX(100%)

这会将 div 向右移动 100%,但仅在硬件加速设备中效果良好且平滑。

You can also use translateX. This defintely hardware accelerates.

targetDiv.style.webkitTransition = "0ms"

targetDiv.style.webkitTransform = "translateX(100%)

This would move a div to the right by 100% but nice and smooth only in hardware accelerated devices.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文