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.
Created
January 3, 2020 18:24
-
-
Save paulobunga/d7c05c537fb50bd38f815f1aa147b15d to your computer and use it in GitHub Desktop.
Countdown timer with SVG circle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="countdown"> | |
<div id="countdown-number"></div> | |
<svg> | |
<circle r="18" cx="20" cy="20"></circle> | |
</svg> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var countdownNumberEl = document.getElementById('countdown-number'); | |
var countdown = 1340; | |
countdownNumberEl.textContent = countdown; | |
setInterval(function() { | |
countdown = --countdown <= 0 ? 10 : countdown; | |
countdownNumberEl.textContent = countdown; | |
}, 12500); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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