A Pen by artemijeka on CodePen.
Created
September 7, 2019 10:18
-
-
Save ArtemSites/95b04e0be1fc48014e96e2ab961ffd1b to your computer and use it in GitHub Desktop.
SVG_circle_progress_bar
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
<svg class="progress-bar" width="120" height="120"> | |
<circle class="progress-bar__circle" stroke="#ff6e00" stroke-width="4" cx="60" cy="60" r="52" fill="transparent"/> | |
</svg> | |
<input type="number" id="inputPercent" value="0"> |
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
const circle = document.querySelector('.progress-bar__circle'); | |
const radius = circle.r.baseVal.value; | |
// console.log(circle.r); | |
const circumference = 2*Math.PI * radius; | |
let inputPercent = document.getElementById('inputPercent'); | |
console.log(inputPercent); | |
inputPercent.addEventListener('change', function() { | |
setProgressBar(inputPercent.value); | |
}); | |
circle.style.strokeDasharray = `${circumference} ${circumference}`; | |
circle.style.strokeDashoffset = circumference; | |
function setProgressBar(percent) { | |
let offset = circumference - percent / 100 * circumference; | |
circle.style.strokeDashoffset = offset; | |
} |
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
html, | |
body { | |
display: flex; | |
justify-content: center; | |
height: 100vh; | |
align-items: center; | |
background-color: #43a047; | |
} | |
.progress-bar__circle { | |
transform-origin: center; | |
transform: rotate(-90deg); | |
transition: stroke-dashoffset 500ms; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment