如果值大于最大值,请更改进度条颜色



如果进度条的值大于设置的最大值,如何更改进度条的颜色。这意味着进度条上的标签可以超过为进度条设置的最大值,并且如果该值大于最大值,则颜色将变为红色。如果该值大于最大值,则更改为红色,否则更改为蓝色。

code:
https://jsbin.com/hamehel/1/edit?html,output

使用一个比较progress.max和progress.value的标志怎么样?

const progress = document.querySelector('#progress');
const up10 = () => {
progress.value += 10;
changeStyle();
};
const down10 = () => {
progress.value -= 10;
changeStyle();
};
const changeStyle = () => {
if (progress.max > progress.value) {
progress.classList.remove('progress-max')
} else {
progress.classList.add('progress-max')
}
}
changeStyle();
.progress-max {
color: lightblue;
}
.progress-max::-moz-progress-bar {
background: red;
}
.progress-max::-webkit-progress-bar {
background: red;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<body>
<progress id="progress" value="100" max="100"></progress>
<button onclick="up10()">+ 10</button>
<button onclick="down10()">-10</button>
</body>
</html>

最新更新