Changing color based on gauge value – Javascript 根据仪表值改变颜色 – JavaScript

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

Changing color based on gauge value - Javascript

问题

I'm trying to write a Javascript function to go alongside a gauge that I copied online, the gauge works like this:

const gaugeElement = document.querySelector(".gauge");

function setGaugeValue(gauge, value) {
  if (value < 0 || value > 1) {
    return;
  }

  gauge.querySelector(".gauge__fill").style.transform = `rotate(${value / 2}turn)`;
  gauge.querySelector(".gauge__cover").textContent = `${Math.round(value * 100)}%`;
}

setGaugeValue(gaugeElement, 0.04);
.gauge {
    width: 100%;
    max-width: 250px;
    font-family: 'Lato', SansSerif;
    font-size: 32px;
}

.gauge__body {
    width: 100%;
    height: 0;
    padding-bottom: 50%;
    background: #b4c0be;
    position: relative;
    left: 35px;
    border-top-left-radius: 100% 200%;
    border-top-right-radius: 100% 200%;
    overflow: hidden;
}

.gauge__fill {
    position: absolute;
    top: 100%;
    left: 0;
    width: inherit;
    height: 100%;
    background-color: #5bb62a;
    transform-origin: center top;
    transform: rotate(0.25turn);
    transition: transform 0.2s ease-out;
}

.gauge__cover {
    width: 75%;
    height: 150%;
    background: #f5f6fa;
    position: absolute;
    border-radius: 50%;
    top: 25%;
    left: 50%;
    transform: translateX(-50%);

    display: flex;
    align-items: center;
    justify-content: center;
    padding-bottom: 25%;
    box-sizing: border-box;
}
<div class="card">
  <div class="FuelLevelText">
    <h3>Fuel Level</h3>
  </div>
  <div class="gauge">
    <div class="gauge__body">
      <div id="gaugecolor" class="gauge__fill"></div>
      <div class="gauge__cover"></div>
    </div>
  </div>
</div>

There's also a bit of CSS that goes with this.

This works fine to change the gaugeElement value and will change the percentage on screen, however I'd like to add a rule so if the value goes below 0.05 or 5% the gauge fill background-color turns red to indicate it's running low. I'm very very new to coding so I'm struggling to find a resource online that has something similar to what I need that I can adapt to my needs.

I tried adding a line:

if (gaugeElement, < 0.05) {
    function changebackground(color) {
        #gaugecolor = color;
    }

    changebackground('red');
}

However I don't know enough Javascript to know if this is even targeting the right section?

英文:

I'm trying to write a Javascript function to go alongside a gauge that I copied online, the gauge works like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const gaugeElement = document.querySelector(&quot;.gauge&quot;);

function setGaugeValue(gauge, value) {
  if (value &lt; 0 || value &gt; 1) {
    return;
  }

  gauge.querySelector(&quot;.gauge__fill&quot;).style.transform = `rotate(${value / 2}turn)`;
  gauge.querySelector(&quot;.gauge__cover&quot;).textContent = `${Math.round(value * 100)}%`;

}

setGaugeValue(gaugeElement, 0.04);

<!-- language: lang-css -->

.gauge {
    width: 100%;
    max-width: 250px;
    font-family: &#39;Lato&#39;, SansSerif;
    font-size: 32px;

}

.gauge__body {
    width: 100%;
    height: 0;
    padding-bottom: 50%;
    background: #b4c0be;
    position: relative;
    left: 35px;
    border-top-left-radius: 100% 200%;
    border-top-right-radius: 100% 200%;
    overflow: hidden;

}

.gauge__fill {
    position: absolute;
    top: 100%;
    left: 0;
    width: inherit;
    height: 100%;
    background-color: #5bb62a;
    transform-origin: center top;
    transform: rotate(0.25turn);
    transition: transform 0.2s ease-out;
}

.gauge__cover {
    width: 75%;
    height: 150%;
    background: #f5f6fa;
    position: absolute;
    border-radius: 50%;
    top: 25%;
    left: 50%;
    transform: translateX(-50%);

    display: flex;
    align-items: center;
    justify-content: center;
    padding-bottom: 25%;
    box-sizing: border-box;

}

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

&lt;div class=&quot;card&quot;&gt;
  &lt;div class=&quot;FuelLevelText&quot;&gt;
    &lt;h3&gt;Fuel Level&lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class=&quot;gauge&quot;&gt;
    &lt;div class=&quot;gauge__body&quot;&gt;
      &lt;div id=&quot;gaugecolor&quot; class=&quot;gauge__fill&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;gauge__cover&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

There's also a bit of CSS that goes with this

This works fine to change the gaugeElement value and will change the percentage on screen, however I'd like to add a rule so if the value goes below 0.05 or 5% the gauge fill background-color turns red to indicate it's running low. I'm very very new to coding so I'm struggling to find a resource online that has something similar to what I need that I can adapt to my needs.

I tried adding a line:

if (gaugeElement, &#39;&lt; 0.05&#39;) {
    function changebackground(color) {
        #gaugecolor = color;
    }

    changebackground(&#39;red&#39;);
}

However I don't know enough Javascript to know if this is even targeting the right section?

答案1

得分: 0

如果数值低于0.05或5%,仪表填充的背景颜色会变为红色。

英文:

If the value goes below 0.05 or 5% the gauge fill background-color turns red.


const gaugeElement = document.querySelector(&quot;.gauge&quot;);

function setGaugeValue(gauge, value) {
  if (value &lt; 0 || value &gt; 1) {
    return;
  }

  gauge.querySelector(&quot;.gauge__fill&quot;).style.transform = `rotate(${value / 2}turn)`;
  gauge.querySelector(&quot;.gauge__cover&quot;).textContent = `${Math.round(value * 100)}%`;

  if (value &lt; 0.05) {
    gauge.querySelector(&quot;.gauge__fill&quot;).style.backgroundColor = &#39;red&#39;;
  } else {
    gauge.querySelector(&quot;.gauge__fill&quot;).style.backgroundColor = &#39;&#39;; // Reset the background color
  }
}

setGaugeValue(gaugeElement, 0.26);

huangapple
  • 本文由 发表于 2023年5月22日 17:38:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304838.html
匿名

发表评论

匿名网友

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

确定