英文:
How do I make a circular border in CSS in which x% is red and the rest is white, with x being a state variable in a javascript file?
问题
我正在制作一个计时器应用程序,在其中最初有一个白色的环绕剩余时间的环。我想要将环绕剩余时间的环逐渐变红,当时间用尽时完全变红。
<div className="countdown-timer">
<span>{remainingTime.hours}</span>
<span>:</span>
<span>{remainingTime.minutes}</span>
<span>:</span>
<span>{remainingTime.seconds}</span>
</div>
.countdown-timer {
width: 400px;
height: 400px;
border: 6px solid white;
border-radius: 50%;
box-shadow: black;
font-family: "DM Sans";
font-size: 72px;
display: flex;
justify-content: center;
align-items: center;
margin-top: 10px;
margin-left: auto;
margin-right: auto;
}
我尝试了一种同心圆的方法,但没有得到我想要的效果。
这是我想要的效果:https://i.stack.imgur.com/AVOz3.png
英文:
I am making a timer app in which there is initially a white ring surrounding the time remaining. I want to color the ring surrounding the time remaining to slowly turn red, with it being completely red as the time runs out.
<div className="countdown-timer">
<span>{remainingTime.hours}</span>
<span>:</span>
<span>{remainingTime.minutes}</span>
<span>:</span>
<span>{remainingTime.seconds}</span>
</div>
.countdown-timer {
width: 400px;
height: 400px;
border: 6px solid white;
border-radius: 50%;
box-shadow: black;
font-family: "DM Sans";
font-size: 72px;
display: flex;
justify-content: center;
align-items: center;
margin-top: 10px;
margin-left: auto;
margin-right: auto;
}
I tried an approach with concentric circles but did not get the intended effect I wanted.
This is what I am going for: https://i.stack.imgur.com/AVOz3.png
答案1
得分: 0
Sure, here is the translated code:
<div>
{remaining.Time.seconds > X.time ? (
<div className="countdown-timer">
<span>{remainingTime.hours}</span>
<span>:</span>
<span>{remainingTime.minutes}</span>
<span>:</span>
<span>{remainingTime.seconds}</span>
</div>
) : (
<div className="countdown-timer2">
<span>{remainingTime.hours}</span>
<span>:</span>
<span>{remainingTime.minutes}</span>
<span>:</span>
<span>{remainingTime.seconds}</span>
</div>
)}
</div>
CSS部分:
.countdown-timer {
width: 400px;
height: 400px;
border: 6px solid white;
border-radius: 50%;
box-shadow: black;
font-family: "DM Sans";
font-size: 72px;
display: flex;
justify-content: center;
align-items: center;
margin-top: 10px;
margin-left: auto;
margin-right: auto;
}
.countdown-timer2 {
width: 400px;
height: 400px;
border: 6px solid white;
border-radius: 50%;
box-shadow: black;
border-color: red;
font-family: "DM Sans";
font-size: 72px;
display: flex;
justify-content: center;
align-items: center;
margin-top: 10px;
margin-left: auto;
margin-right: auto;
}
Please note that I've left the variable X.time
and {remainingTime}
unchanged in the translated code as you requested.
英文:
<div>
{remaining.Time.seconds > X.time?(
<div className="countdown-timer">
<span>{remainingTime.hours}</span>
<span>:</span>
<span>{remainingTime.minutes}</span>
<span>:</span>
<span>{remainingTime.seconds}</span>
</div>):(
<div className="countdown-timer2">
<span>{remainingTime.hours}</span>
<span>:</span>
<span>{remainingTime.minutes}</span>
<span>:</span>
<span>{remainingTime.seconds}</span>
</div>
)
}
</div>
---------------------------CSS File----------------------------------------
.countdown-timer {
width: 400px;
height: 400px;
border: 6px solid white;
border-radius: 50%;
box-shadow: black;
font-family: "DM Sans";
font-size: 72px;
display: flex;
justify-content: center;
align-items: center;
margin-top: 10px;
margin-left: auto;
margin-right: auto;
}
.countdown-timer2 {
width: 400px;
height: 400px;
border: 6px solid white;
border-radius: 50%;
box-shadow: black;
border-color: red;
font-family: "DM Sans";
font-size: 72px;
display: flex;
justify-content: center;
align-items: center;
margin-top: 10px;
margin-left: auto;
margin-right: auto;
}
where X.time is the time above which it should remain white while less than that it will turn red
答案2
得分: 0
尝试使用conic-gradient
<!-- 开始代码片段: js 隐藏: false 控制台: true babel: true -->
<!-- 语言: lang-js -->
function Timer() {
const [remainingTime, setRemainingTime] = React.useState({
hours: 0,
minutes: 0,
seconds: 0
});
return (
<div className="countdown-timer" style={{ "--x": 0.75 }}>
<span>{remainingTime.hours}</span>
<span>:</span>
<span>{remainingTime.minutes}</span>
<span>:</span>
<span>{remainingTime.seconds}</span>
</div>
);
}
ReactDOM.createRoot(document.getElementById("app")).render(<Timer />);
<!-- 语言: lang-css -->
:root {
--bg: purple;
}
body {
background: var(--bg);
}
.countdown-timer {
width: 400px;
aspect-ratio: 1;
border-radius: 50%;
box-shadow: black;
font-family: "DM Sans";
font-size: 72px;
display: flex;
justify-content: center;
align-items: center;
margin-top: 10px;
margin-left: auto;
margin-right: auto;
position: relative;
--x: 0;
--deg: calc(calc(1 - var(--x)) * 360deg);
background: conic-gradient(white 0deg, white var(--deg), var(--deg), red 360deg);
}
.countdown-timer::before {
content: '';
position: absolute;
inset: 6px;
border-radius: 50%;
background-color: var(--bg);
}
.countdown-timer span {
position: relative;
z-index: 1;
}
<!-- 语言: lang-html -->
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<div id="app"></div>
<!-- 结束代码片段 -->
英文:
Try conic-gradient
<!-- begin snippet: js hide: false console: true babel: true -->
<!-- language: lang-js -->
function Timer() {
const [remainingTime, setRemainingTime] = React.useState({
hours: 0,
minutes: 0,
seconds: 0
});
return ( <
div className = "countdown-timer"
style = {
{
"--x": 0.75
}
} >
<
span > {
remainingTime.hours
} < /span> <
span >: < /span> <
span > {
remainingTime.minutes
} < /span> <
span >: < /span> <
span > {
remainingTime.seconds
} < /span> <
/div>
);
}
ReactDOM.createRoot(document.getElementById("app")).render( < Timer / > );
<!-- language: lang-css -->
:root {
--bg: purple;
}
body {
background: var(--bg);
}
.countdown-timer {
width: 400px;
aspect-ratio: 1;
border-radius: 50%;
box-shadow: black;
font-family: "DM Sans";
font-size: 72px;
display: flex;
justify-content: center;
align-items: center;
margin-top: 10px;
margin-left: auto;
margin-right: auto;
position: relative;
--x: 0;
--deg: calc(calc(1 - var(--x)) * 360deg);
background: conic-gradient(white 0deg, white var(--deg), var(--deg), red 360deg);
}
.countdown-timer::before {
content: '';
position: absolute;
inset: 6px;
border-radius: 50%;
background-color: var(--bg);
}
.countdown-timer span {
position: relative;
z-index: 1;
}
<!-- language: lang-html -->
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<div id="app"></div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论