我正在try 使用CSS和JavaScript(react too),使SVG圆形进度条根据线性渐变的进度/百分比改变 colored颜色 .
到目前为止,我找到的每个解决方案都有硬编码的 colored颜色 代码或使用jQuery,但我无法使用.
<svg viewBox="0 0 36 36" className="circular-chart">
<path className="svg-back"
fill='none'
stroke={props.theme.colors.Neutral.Gray1}
strokeWidth={3}
d="M18 2.0845
a 15.9155 15.9155 0 0 1 0 31.831
a 15.9155 15.9155 0 0 1 0 -31.831"
/>
<path className="circle"
stroke-dasharray={`${props.percentage} 100`}
d="M18 2.0845
a 15.9155 15.9155 0 0 1 0 31.831
a 15.9155 15.9155 0 0 1 0 -31.831"
/>
<text
x={18}
y={21}
className="svg-circle-text">
{props.percentage}%
</text>
</svg>
CSS:
.svg-circle-text {
font-size: 0.5rem;
text-anchor: middle;
fill: ${theme.colors.Primary.Black}
font-weight: bold;
}
.circular-chart {
display: block;
margin: 10px auto;
max-width: 80%;
max-height: 250px;
}
.circle {
stroke: #99DA98;
fill: none;
stroke-width: 3;
stroke-linecap: round;
animation: progress 1s ease-out forwards;
}
@keyframes progress {
0% {
stroke-dasharray: 0 100;
}
}
在另一个组件中,我有以下背景,这是这个循环进度条的关键:
background: linear-gradient(
90deg,
#99da98 0%,
#f8b66a 51.56%,
#cf6767 100%
);
因此,如果百分比是,比如说,60%,它应该显示梯度在60%的值.如果能帮点忙,我们将不胜感激.谢谢