我可以修改我的 CSS 关键帧动画以使其在移动设备上更流畅吗?

问题描述 投票:0回答:0

我基于这个 CodePen 倒数计时器,我注意到它在使用硬件加速的浏览器和设备上很流畅,但在移动/仅 CPU 浏览器上闪烁。谁能帮我修改 CSS 使其在所有设备上看起来都不错?

CSSS(少):

html { height: 100%; }
body { 
  height: 100%;
  // display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  // background: #EEE;
}

@halfHeight: 0.72em;
@borderRadius: 0.15em;

.container {
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  width: 100%;
  height: 0;
  padding-top: 50%; /* adjust to your desired aspect ratio */
  // background-repeat: no-repeat;
  // height: auto;
  // width: 100%;
  // background-size: cover;
  // background-size: 100% 100%;
  // background-position: left top;
  position: relative;
  z-index: -1;
}



.card-labels-background {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -100%);
  width: 65%;
  background-color: black;
  border-radius: 1.15em 1.15em 0 0;
  height: 120px;
}

.event-text-box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, 40%);
  width: 55%; /* adjust to your desired width */
  background-color: black;
  padding-top: 50px;
  padding-bottom: 10px;
  // padding-left: 10px;
  // padding-right: 10px;
  color: #FFF;
  font-size: 5vw;
  border-radius: 0 0 @borderRadius @borderRadius;
  text-align: center;
}

.flip-clock {
  text-align: center;
  perspective: 400px;
  margin: 20px auto;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 100%;
  
  *,
  *:before,
  *:after { box-sizing: border-box; }
}

.flip-clock__piece {
  display: inline-block;
  margin: 0 5px;
}

.flip-clock__slot {
  font-size: 2vw;
  color: #FFF;
}

.card {
  display: block;
  position: relative; 
  padding-bottom: @halfHeight;
  font-size: 9vw;
  line-height: 0.95;
}

.card__top,
.card__bottom,
.card__back::before,
.card__back::after {
  display: block;
  height: @halfHeight;
  color: #FFF;
  background: #FB0038;
  padding: 0.25em 0.25em;
  border-radius: @borderRadius @borderRadius 0 0;
  backface-visiblity: hidden;
  transform-style: preserve-3d;
  width: 1.8em;
  transform: translateZ(0);
}

.card__bottom { 
  color: #FFF;
  position: absolute;
  top: 50%;
  left: 0;
  // border-top: solid 1px transparent;
  background: #CD0027; 
  border-radius: 0 0 @borderRadius @borderRadius; 
  pointer-events: none;
  overflow: hidden;
  backface-visiblity: hidden;
}

.card__bottom::after {
  display: block;
  margin-top: -@halfHeight;
  // backface-visiblity: hidden;
}

.card__back::before,
.card__bottom::after {
  content: attr(data-value);
  backface-visiblity: hidden;
}

.card__back {
  position: absolute;
  top: 0;
  height: 100%;
  left: 0%;
  pointer-events: none;
  backface-visiblity: hidden;
}

.card__back::before {
  position: relative;
  z-index: -1;
  overflow: hidden;
  backface-visiblity: hidden;
}

.flip .card__back::before {
  animation: flipTop 0.3s cubic-bezier(.37,.01,.94,.35);
  animation-fill-mode: both;
  transform-origin: center bottom;
  backface-visiblity: hidden;
}

.flip .card__back .card__bottom {
  transform-origin: center top;
  animation-fill-mode: both;
  animation: flipBottom 0.6s cubic-bezier(.15,.45,.28,1);// 0.3s; 
}

@keyframes flipTop {
  0% {
    transform: rotateX(0deg);
    z-index: 2;
  }
  0%, 99% {
    opacity: 0.99;
  }
  100% {
    transform: rotateX(-90deg);
    opacity: 0;
  }
}

@keyframes flipBottom {
  0%, 50% {
    z-index: -1;
    transform: rotateX(90deg);
    opacity: 0;
  }
  51% {
    opacity: 0.99;
  }
  100% {
    opacity: 0.99;
    transform: rotateX(0deg);
    z-index: 5;
  }
}

Javascript:

console.clear();


function CountdownTracker(label, value){

  var el = document.createElement('span');

  el.className = 'flip-clock__piece';
  el.innerHTML = '<span class="flip-clock__slot">' + label + '</span>' + 
    '<b class="flip-clock__card card"><b class="card__top"></b><b class="card__bottom"></b><b class="card__back"><b class="card__bottom"></b></b></b>';

  this.el = el;

  var top = el.querySelector('.card__top'),
      bottom = el.querySelector('.card__bottom'),
      back = el.querySelector('.card__back'),
      backBottom = el.querySelector('.card__back .card__bottom');

  this.update = function(val){
    val = ( '0' + val ).slice(-2);
    if ( val !== this.currentValue ) {
      
      if ( this.currentValue >= 0 ) {
        back.setAttribute('data-value', this.currentValue);
        bottom.setAttribute('data-value', this.currentValue);
      }
      this.currentValue = val;
      top.innerText = this.currentValue;
      backBottom.setAttribute('data-value', this.currentValue);

      this.el.classList.remove('flip');
      void this.el.offsetWidth;
      this.el.classList.add('flip');
    }
  }
  
  this.update(value);
}

// Calculation adapted from https://www.sitepoint.com/build-javascript-countdown-timer-no-dependencies/

function getTimeRemaining(endtime) {
  var t = Date.parse(endtime) - Date.parse(new Date());
  return {
    'Total': t,
    'days': Math.floor(t / (1000 * 60 * 60 * 24)),
    'hours': Math.floor((t / (1000 * 60 * 60)) % 24),
    'minutes': Math.floor((t / 1000 / 60) % 60),
    'seconds': Math.floor((t / 1000) % 60)
  };
}

function getTime() {
  var t = new Date();
  return {
    'Total': t,
    'hours': t.getHours() % 12,
    'minutes': t.getMinutes(),
    'seconds': t.getSeconds()
  };
}

function Clock(countdown,callback) {
  
  countdown = countdown ? new Date(Date.parse(countdown)) : false;
  callback = callback || function(){};
  
  var updateFn = countdown ? getTimeRemaining : getTime;

  this.el = document.createElement('div');
  this.el.className = 'flip-clock';

  var trackers = {},
      t = updateFn(countdown),
      key, timeinterval;

  for ( key in t ){
    if ( key === 'Total' ) { continue; }
    trackers[key] = new CountdownTracker(key, t[key]);
    this.el.appendChild(trackers[key].el);
  }

  var i = 0;
  function updateClock() {
    timeinterval = requestAnimationFrame(updateClock);
    
    // throttle so it's not constantly updating the time.
    if ( i++ % 10 ) { return; }
    
    var t = updateFn(countdown);
    if ( t.Total < 0 ) {
      cancelAnimationFrame(timeinterval);
      for ( key in trackers ){
        trackers[key].update( 0 );
      }
      callback();
      return;
    }
    
    for ( key in trackers ){
      trackers[key].update( t[key] );
    }
  }

  setTimeout(updateClock,500);
}

HTML:

<script>
        var deadline = new Date(Date.parse(new Date()) + 99 * 24 * 60 * 60 * 1000);
        var c = new Clock(deadline, function(){});
        document.getElementsByClassName("container")[0].appendChild(c.el);
    </script>

我尝试了

backface-visibility: hidden;
并将所有转换更改为
translate3d
以鼓励硬件加速但没有运气。

css animation css-animations hardware-acceleration
© www.soinside.com 2019 - 2024. All rights reserved.