使用CSS box-shadow进行脉冲动画会占用相当大的CPU

blu*_*ren 2 html css css3 twitter-bootstrap box-shadow

需要在标签上显示脉冲动画以突出显示未完成的通知.我在网上进行研究时遇到过这种情况并实施了同样的方法.它运行良好,但是,当标签脉冲时,CPU使用率恒定为50%,而通常几乎没有任何东西.我想了解这是怎样或为什么发生的,如果可能的话,我想了解一下.

CSS:

.pulse {
   /*margin:100px;*/
   display: block;
   /*width: 22px;*/
   /*height: 22px;*/
   /*border-radius: 100%;*/
   background: #cca92c;
   cursor: pointer;
   box-shadow: 0 0 0 rgba(204,169,44, 0.4);
   animation: pulse 2s infinite;
 }

 .pulse:hover {
   animation: none;
 }

 @-webkit-keyframes pulse {
   0% {
     -webkit-box-shadow: 0 0 0 0 rgba(204,169,44, 0.4);
   }
   70% {
       -webkit-box-shadow: 0 0 0 10px rgba(204,169,44, 0);
   }
   100% {
       -webkit-box-shadow: 0 0 0 0 rgba(204,169,44, 0);
   }
 }

 @keyframes pulse {
   0% {
     -moz-box-shadow: 0 0 0 0 rgba(204,169,44, 0.4);
     box-shadow: 0 0 0 0 rgba(196,33,61, 0.85);
   }
   70% {
       -moz-box-shadow: 0 0 0 10px rgba(204,169,44, 0);
       box-shadow: 0 0 0 10px rgba(204,169,44, 0);
   }
   100% {
       -moz-box-shadow: 0 0 0 0 rgba(204,169,44, 0);
       box-shadow: 0 0 0 0 rgba(204,169,44, 0);
   }
 }
Run Code Online (Sandbox Code Playgroud)

HTML:

    <a class="dropdown-toggle" data-toggle="dropdown" id="alertingDashboardDropdown" href="#">Alerts Dashboard
                    <span class="label {{alertCount.totalOpenAlertsLabelClass}} dropdown-subscript-white pull-right">{{alertCount.totalOpenAlerts}}</span>
                </a>
.
.
Run Code Online (Sandbox Code Playgroud)

{{alertCount.totalOpenAlertsLabelClass}} 基本上返回"标签 - 主脉冲"

Ric*_*iro 7

发生这种情况是因为您使用的box-shadow对动画不是很好,因为它会触发重绘.

您可以在此处查看哪些属性触发布局,绘制和复合更改.

最好的是opacitytransform,也许你可以改变你的动画中使用的.

编辑:创建2个jsfiddle,一个用box-shadow,另一个用transform.我编辑了您的代码,但您可以检查性能,研究它,了解变化并根据您的需求进行调整.

我是怎么做到的

添加了一个:before伪元素,该元素将在该数字下运行动画.will-change: transform;告诉浏览器元素的转换将发生变化,浏览器会对其进行优化.

.pulse {
  position: relative;
  display: inline-block;
  width: 22px; 
  height: 22px;
  line-height: 22px;
  border-radius: 100%;
  background-color: #cca92c;
  text-align: center;
  cursor: pointer; 
}

.pulse:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%; 
  height: 100%;
  background-color: #cca92c;
  border-radius: 100%; 
  z-index: -1;
  animation: pulse 2s infinite; 
  will-change: transform;
}

.pulse:hover:before {
  animation: none;
}

 @keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(2);
    opacity: 0;
  }
 }
Run Code Online (Sandbox Code Playgroud)
<a class="dropdown-toggle" data-toggle="dropdown" id="alertingDashboardDropdown" href="#">
  <span>Alerts Dashboard</span>
  <span class="pulse">5</span>
</a>
Run Code Online (Sandbox Code Playgroud)