Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save paulobunga/d7c05c537fb50bd38f815f1aa147b15d to your computer and use it in GitHub Desktop.
Save paulobunga/d7c05c537fb50bd38f815f1aa147b15d to your computer and use it in GitHub Desktop.
Countdown timer with SVG circle

Countdown timer with SVG circle

I used stroke-dasharray and stroke-dashoffset in a circle svg element. Setting stroke-dasharray to a full circle (e.g. 113px) and animating the stroke-dashoffset from 0px to 113px makes the circle's stroke disappear gradually.

A Pen by ไน้ท์นี on CodePen.

License.

<div id="countdown">
<div id="countdown-number"></div>
<svg>
<circle r="18" cx="20" cy="20"></circle>
</svg>
</div>
var countdownNumberEl = document.getElementById('countdown-number');
var countdown = 1340;
countdownNumberEl.textContent = countdown;
setInterval(function() {
countdown = --countdown <= 0 ? 10 : countdown;
countdownNumberEl.textContent = countdown;
}, 12500);
body {
height: 100%;
width: 100%;
background-color: #333;
}
#countdown {
position: relative;
margin: auto;
margin-top: 100px;
height: 40px;
width: 40px;
text-align: center;
}
#countdown-number {
color: white;
display: inline-block;
line-height: 40px;
}
svg {
position: absolute;
top: 0;
right: 0;
width: 40px;
height: 40px;
transform: rotateY(-180deg) rotateZ(-90deg);
}
svg circle {
stroke-dasharray: 113px;
stroke-dashoffset: 0px;
stroke-linecap: round;
stroke-width: 2px;
stroke: white;
fill: none;
animation: countdown 10s linear infinite forwards;
}
@keyframes countdown {
from {
stroke-dashoffset: 0px;
}
to {
stroke-dashoffset: 113px;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment