首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CSS动画性能较差-没有浏览器绘制

CSS动画性能较差-没有浏览器绘制
EN

Stack Overflow用户
提问于 2018-08-08 16:17:45
回答 1查看 155关注 0票数 3

我有许多我正在制作动画的元素,我开发的方式不应该导致任何浏览器绘制。如果我在Chrome Devtools中打开“油漆闪烁”,我根本看不到任何油漆闪烁。然而,如果我记录下性能,那么图表显示有很多时间花在绘画上。FPS有时低至15fps。

我实际上是在Vue中构建的,编译后的代码导致太多代码无法粘贴到这里。我意识到动画有点问题,我仍然需要做一些计时等工作-但对于这个问题的目的,我只关心性能。

我已经在CodePen上发布了编译后的代码:

https://codepen.io/IOIIOOIO/pen/gjBqyg

似乎StackOverflow需要我在这里发布一些代码,所以这里只有一个元素的编译代码:

代码语言:javascript
运行
复制
.circle {
  position: relative;
  width: 100%;
  padding-top: 100%;
  border-radius: 50%;
  overflow: hidden;
}

.circle::before {
    content: "";
    background-color: black;
    position: absolute;
    top: 0;
    left: 0;
    width: 50%;
    height: 100%;
    animation-name: switch;
    animation-iteration-count: infinite;
    animation-timing-function: steps(1);
    animation-duration: 3s;
    animation-delay: inherit;
  }

.rotating-circle {
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-radius: 50%;
}

.rotating-circle--first-cycle {
    background-color: black;
    animation-name: rotate, toggle-first;
    animation-duration: 3s, 3s;
    animation-iteration-count: infinite, infinite;
    animation-timing-function: linear, steps(1);
    animation-delay: 1800ms;
  }

.rotating-circle--second-cycle {
    opacity: 0;
    animation-name: rotate, toggle-second;
    animation-duration: 3s, 3s;
    animation-iteration-count: infinite, infinite;
    animation-timing-function: linear, steps(1);
    animation-delay: 1800ms;
  }


@keyframes rotate {
  0% {
    transform: rotate3d(0, 1, 0, 0deg);
  }
  50% {
    transform: rotate3d(0, 1, 0, 180deg);
  }
}

@keyframes toggle-first {
  0% {
    opacity: 1;
  }
  25% {
    opacity: 0;
  }
  75% {
    opacity: 1;
  }
}

@keyframes toggle-second {
  0% {
    opacity: 0;
  }
  25% {
    opacity: 1;
  }
  75% {
    opacity: 0;
  }
}

@keyframes switch {
  0% {
    transform: translatex(0);
  }
  50% {
    transform: translatex(100%);
  }
  100% {
    transform: translatex(0);
  }
}
代码语言:javascript
运行
复制
<div class="circle" style="background-color: rgb(255, 0, 0); animation-delay: 0ms;">
  <div class="rotating-circle rotating-circle--first-cycle" style="animation-delay: 0ms;">
  </div>
  <div class="rotating-circle rotating-circle--second-cycle" style="background-color: rgb(255, 0, 0); animation-delay: 0ms;">
  </div>
</div>

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51741818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档