【问题标题】:Choppy transition: transform scalling animation on Safari断断续续的过渡:在 Safari 上变换缩放动画
【发布时间】:2021-06-20 13:05:48
【问题描述】:

我很难在 Safari(移动设备)中制作不连贯的缩放动画。断断续续的意思是你可以清楚地看到它不是 60FPS 流畅的动画。我需要说元素是绝对定位的,所以它不应该影响布局,不是吗?

我尝试的第一件事是通过创建具有 2 个关键帧的 CSS 动画来为元素设置动画,从 transform: scale(0); 到 transform: scale(1);。对于我想要达到的效果,我也使用了transform-origin: top right;。我也尝试通过设置will-change: transform; 来优化它,但它很不稳定。

我废弃了它并对其进行了重新设计,因此它使用了transition: transform。所以默认情况下元素有transform: scale(0);,当某个类附加到它时,它会得到transform: scale(1);,哪个过渡正在动画,但它仍然不稳定。

经过一些研究,我发现您希望避免对需要浏览器重新计算布局的属性进行动画处理。我发现this site 说 WebKit(据我所知 Safari 正在使用它)几乎会为每个属性更改重新计算布局。是这样吗,如果这是你如何在移动版 Safari 上制作 60FPS 流畅动画(在其他使用 Safari 的平台上这并不明显,因为它们有更多的资源来重新计算所有内容,而且看起来比移动版更流畅)?

【问题讨论】:

  • 关于优化 css 动画等的好文章。 here。您对此无能为力。请记住,同时发生的其他动作(其他动画或 js 脚本)同时工作会降低整体性能并可能导致上述问题。
  • transform: scale(x) 已经是最便宜的动画之一,因此您的问题可能是由您页面上的其他动画引起的。

标签: css webkit css-animations mobile-safari


【解决方案1】:

您可以尝试 2 件事:

  • 避免使用 scale(0)。在某些浏览器中,这会产生问题。 scale(0.01) 几乎一样,对浏览器会更好。

  • 尽量让动画由 GPU 而不是 CPU 来处理。这可以通过以下代码完成

    来自:{transform: scale(0.01) translateZ(1px);}

    to: {transform: scale(1) translateZ(1px);}

【讨论】:

  • 出于某种原因,这似乎更加流畅和更好。我发现的另一件事是自定义缓动也可以产生影响。使用不同的缓动可能看起来不那么不稳定。
【解决方案2】:

参考this article 部分Animate Changes in CSS Properties,您可以使用关键帧制作动画,但需要使用这样的百分比:

w3s article 中的更多示例。

PD:如果您添加示例,我会尽力帮助您。

function animateStart(){
  document.getElementById('ball').classList.add('bounce');
}

function animationPause(){
  document.getElementById('ball').style.webkitAnimationPlayState='paused';
}

function animationContinue(){
  document.getElementById('ball').style.webkitAnimationPlayState='running';
}
@-webkit-keyframes bounce {
    0% {top: 100px; left: 1px; -webkit-animate-timing-function: ease-in;}
    25% {top: 150px; left: 76px; -webkit-animate-timing-function: ease-out;}
    50% {top: 100px; left: 151px -webkit-animate-timing-function: ease-in;}
    75% {top: 150px; left: 226px -webkit-animate-timing-function: ease-out;}
    100% {top:100px; left: 301px;}
    }
 
.bounce {
    -webkit-animation-name: bounce;
    -webkit-animation-duration: 2s;
    -webkit-animation-timing-function: linear;
    -webkit-animation-iteration-count: infinite;
    -webkit-animation-direction: alternate;
    }
    
.ball-style {
    position:absolute; top: 100px; left: 10px;
    height:100px; width:100px; border-radius:50px;
    background:-webkit-radial-gradient(30% 30%, white, red 10%, black);;
}

.wall {
    position:absolute; left: 400px; top: 100px;
    height: 150px;
    background: black;
}
<input type="button" value="Animate alternative"
       onclick="document.getElementById('ball').classList.add('bounce');">
       
<input type="button" value="Animate" onclick="animateStart()">
<input type="button" value="Pause" onclick="animationPause()">
<input type="button" value="Continue" onclick="animationContinue()">

<div id="ball" class="ball-style"></div>
 
<div class="wall">&nbsp;</div>

【讨论】:

    【解决方案3】:

    我不知道这是否有帮助,但您是否使用 javascript 添加类并通过更改类来启动动画? 例如,jQuery 在操作元素时会在 dom 中导致长深度分析,这可能会干扰您实现所需 fps 所需的性能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 2011-05-25
      相关资源
      最近更新 更多