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?

huangapple go评论79阅读模式
英文:

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.

&lt;div className=&quot;countdown-timer&quot;&gt;
     &lt;span&gt;{remainingTime.hours}&lt;/span&gt;
     &lt;span&gt;:&lt;/span&gt;
     &lt;span&gt;{remainingTime.minutes}&lt;/span&gt;
     &lt;span&gt;:&lt;/span&gt;
     &lt;span&gt;{remainingTime.seconds}&lt;/span&gt;
&lt;/div&gt;
.countdown-timer {
    width: 400px;
    height: 400px;
    border: 6px solid white;
    border-radius: 50%;
    box-shadow: black;

    font-family: &quot;DM Sans&quot;;
    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.

英文:
&lt;div&gt;
  {remaining.Time.seconds &gt; X.time?(
  &lt;div className=&quot;countdown-timer&quot;&gt;
      &lt;span&gt;{remainingTime.hours}&lt;/span&gt;
      &lt;span&gt;:&lt;/span&gt;
      &lt;span&gt;{remainingTime.minutes}&lt;/span&gt;
      &lt;span&gt;:&lt;/span&gt;
      &lt;span&gt;{remainingTime.seconds}&lt;/span&gt;
  &lt;/div&gt;):(
    &lt;div className=&quot;countdown-timer2&quot;&gt;
      &lt;span&gt;{remainingTime.hours}&lt;/span&gt;
      &lt;span&gt;:&lt;/span&gt;
      &lt;span&gt;{remainingTime.minutes}&lt;/span&gt;
      &lt;span&gt;:&lt;/span&gt;
      &lt;span&gt;{remainingTime.seconds}&lt;/span&gt;
  &lt;/div&gt;

  )
  }
&lt;/div&gt;

---------------------------CSS File----------------------------------------

.countdown-timer {
    width: 400px;
    height: 400px;
    border: 6px solid white;
    border-radius: 50%;
    box-shadow: black;

    font-family: &quot;DM Sans&quot;;
    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: &quot;DM Sans&quot;;
    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 ( &lt;
    div className = &quot;countdown-timer&quot;
    style = {
      {
        &quot;--x&quot;: 0.75
      }
    } &gt;
    &lt;
    span &gt; {
      remainingTime.hours
    } &lt; /span&gt; &lt;
    span &gt;: &lt; /span&gt; &lt;
    span &gt; {
      remainingTime.minutes
    } &lt; /span&gt; &lt;
    span &gt;: &lt; /span&gt; &lt;
    span &gt; {
      remainingTime.seconds
    } &lt; /span&gt; &lt;
    /div&gt;
  );
}
ReactDOM.createRoot(document.getElementById(&quot;app&quot;)).render( &lt; Timer / &gt; );

<!-- 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: &quot;DM Sans&quot;;
  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: &#39;&#39;;
  position: absolute;
  inset: 6px;
  border-radius: 50%;
  background-color: var(--bg);
}

.countdown-timer span {
  position: relative;
  z-index: 1;
}

<!-- language: lang-html -->

&lt;script crossorigin src=&quot;https://unpkg.com/react@18/umd/react.development.js&quot;&gt;&lt;/script&gt;
&lt;script crossorigin src=&quot;https://unpkg.com/react-dom@18/umd/react-dom.development.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;app&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月17日 23:27:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定