Pure CSS countdown animation

前端 未结 1 734
慢半拍i
慢半拍i 2021-02-06 02:39

I\'m trying to do a pure HTML/CSS countdown animation but I\'m struggling to make it work smoothly. For now I\'m only covering minutes and seconds. Notice that I\'m using

1条回答
  •  再見小時候
    2021-02-06 03:09

    This is due to the percentage values of the keyframes. You are using 16% while it should be 16.6667% (100/6) to be more exact and accurate, same thing with the others values.

    When using 360s or 3600s as a duration you will have a clear difference between 16% and 16.6667% thus the state will change earlier and you will have the synchronisation issue. You may not notice this when using 6s as duration with the seconds.

    @keyframes tick6 {  
        0%          { margin-top: 0; }      /* 0*(100/6) */
        16.6667%    { margin-top: -2rem;  } /* 1*(100/6) */
        33.3333%    { margin-top: -4rem;  } /* 2*(100/6) */
        50%         { margin-top: -6rem;  } /* 3*(100/6) */
        66.6667%    { margin-top: -8rem;  } /* 4*(100/6) */
        83.3333%    { margin-top: -10rem; } /* 5*(100/6) */
        100%        { margin-top: -12rem; } /* 6*(100/6) */
    }
    
    @keyframes tick10 {
        0%      { margin-top: 0; }
        10%     { margin-top: -2rem;  }
        20%     { margin-top: -4rem;  }
        30%     { margin-top: -6rem;  }
        40%     { margin-top: -8rem;  }
        50%     { margin-top: -10rem;  }
        60%     { margin-top: -12rem;  }
        70%     { margin-top: -14rem;  }
        80%     { margin-top: -16rem;  }
        90%     { margin-top: -18rem;  }
        100%    { margin-top: -20rem;  }
    }
    
    body {
      background-color: black;
    }
    
    .container {
      background-color: white;
    }
    
    .digit {
      display: inline-block;
      height: 2rem;
      overflow: hidden;
      width: 1ch;
    }
    
    .digit span {
      display: block;
      height: 2rem;
      width: 100%;
    }
    
    .minutes-digit-one {
      animation: tick6 360s infinite step-end;
      /*animation-delay: -54s;*/
    }
    
    .minutes-digit-two {
      animation: tick10 60s infinite step-end;
      /*animation-delay: -54s;*/
    }
    
    .seconds-digit-one {
      animation: tick6 6s infinite step-end;
      /*animation-delay: -54s;*/
    }
    
    .seconds-digit-two {
      animation: tick10 1s infinite step-end;
    }
    5 4 3 2 1 0 5
    9 8 7 6 5 4 3 2 1 0 9
    :
    5 4 3 2 1 0 5
    9 8 7 6 5 4 3 2 1 0 9

    0 讨论(0)
提交回复
热议问题