Home > front end >  Simple Countdown Circle Animation with Pure CSS
Simple Countdown Circle Animation with Pure CSS

Time:10-08

There are many scss samples, but only a few with CSS. Somebody know a simple way to animate a circle countdown made with pure CSS.

CodePudding user response:

Here is a simple and short CSS-only example:

.timer {
    background: -webkit-linear-gradient(left, skyBlue 50%, #eee 50%);
    border-radius: 100%;
    height: calc(var(--size) * 1px);
    width: calc(var(--size) * 1px);
    position: relative;
    -webkit-animation: time calc(var(--duration) * 1s) steps(1000, start) infinite;
      -webkit-mask: radial-gradient(transparent 50%,#000 50%);
      mask: radial-gradient(transparent 50%,#000 50%);
}
.mask {
    border-radius: 100% 0 0 100% / 50% 0 0 50%;
    height: 100%;
    left: 0;
    position: absolute;
    top: 0;
    width: 50%;
    -webkit-animation: mask calc(var(--duration) * 1s) steps(500, start) infinite;
    -webkit-transform-origin: 100% 50%;
}
@-webkit-keyframes time {
    100% {
        -webkit-transform: rotate(360deg);
    }
}
@-webkit-keyframes mask {
    0% {
        background: #eee;
        -webkit-transform: rotate(0deg);
    }
    50% {
        background: #eee;
        -webkit-transform: rotate(-180deg);
    }
    50.01% {
        background: skyBlue;
        -webkit-transform: rotate(0deg);
    }
    100% {
        background: skyBlue;
        -webkit-transform: rotate(-180deg);
    }
}
<div class="timer" style="--duration: 3;--size: 30;">
    <div class="mask"></div>
</div>

<div class="timer" style="--duration: 10;--size: 100;">
    <div class="mask"></div>
</div>

Like shown above, you can control the size and duration with variables.

  • Related