当前位置:网站首页>如何创建一个根据进度改变颜色的进度条
如何创建一个根据进度改变颜色的进度条
2022-07-01 05:16:00 【Phil Arist】
进度条是网页上的常见功能,但是,你知道如何创建一个根据进度自动调整颜色的进度条吗?
效果如下:

需求为:
进度在 80% 和 100% 之间,请将背景颜色设置为绿色。
进度在 60% 到 80% 之间,则将背景颜色设置为蓝色。
进度在 40% 到 60% 之间,则将背景颜色设置为紫色。
进度在 20% 和 40% 之间,则将背景颜色设置为黄色。
进度在 0% 到 20% 之间,则将背景颜色设置为红色。
根据这个需求,我们今天一起来探讨这个内容。
01、创建进度条
首先,我们需要创建一个进度条。通过使用 <progress> 标签创建进度条。
但是在下面的例子中,我们需要使用 CSS 变量,而这个标签是不合适的。 所以我们使用常规的 div 标签和一些 CSS 样式来创建进度条。
进度条包含两个部分:
容器
进展
要创建一个容器,我们只需要创建一个 div 元素:
<div class="bar" style="--percent: 50;"></div>我们使用 CSS 变量百分比来表示进度。
然后我们可以使用伪类元素创建进度部分。
.bar {
height: 20px;
background-color: #f5f5f5;
}
.bar::before {
content: '';
display: flex;
justify-content: end;
width: calc(var(--percent) * 1%);
height: 100%;
background: #2486ff; white-space: nowrap;}

02、换颜色
我们如何实现颜色自动变化的效果?
一个技巧:设置多个背景层,每个背景层都有不同的颜色。 然后我们可以通过调整图层来间接调整背景颜色。
当红色图层和蓝色图层堆叠在一起时,背景为红色。 如果我们再次隐藏红色图层,背景将变为蓝色。
如果我们将 5 层级联在一起,让上层稍微短一点,我们会得到如下效果:
codepen演示效果:https://codepen.io/bytefishmedium/pen/rNdaMYg
03、组合合并
最后,我们需要把前面的代码进行合并,就可以得到一个能自动调整变换颜色的进度条了。
到这里了,我要与你分享的变色进度条的效果就结束了,感谢你的阅读。
边栏推荐
- Use of STM32 expansion board temperature sensor and temperature humidity sensor
- Actual combat: gateway api-2022.2.13
- [daily question in summer] Luogu p5740 [deep foundation 7. Example 9] the best student
- Youqitong [vip] v3.7.2022.0106 official January 22 Edition
- Copy baby prompt: material cannot be empty. How to solve it?
- Global and Chinese market of solder wire 2022-2028: Research Report on technology, participants, trends, market size and share
- [daily question in summer] Luogu p7222 [rc-04] informatics competition
- Global and Chinese market of digital badge 2022-2028: Research Report on technology, participants, trends, market size and share
- AcWing 884. Gauss elimination for solving XOR linear equations
- Unit testing with mongodb
猜你喜欢

在Rainbond中一键部署高可用 EMQX 集群

C# wpf 使用DockPanel实现截屏框

LeetCode522-最长特殊序列II-哈希表-字符串-双指针

Practice of combining rook CEPH and rainbow, a cloud native storage solution

Copier le matériel de conseils de bébé ne peut pas être vide, comment résoudre?

Application of industrial conductive slip ring

实战:redux的基本使用

Daily question -leetcode1175- permutation of prime numbers - Mathematics

How to select conductive slip ring material

Ebpf cilium practice (2) - underlying network observability
随机推荐
3D建模与处理软件简介 刘利刚 中国科技大学
积分商城游戏能够给商家带来什么?怎么搭建积分商城?
One click deployment of highly available emqx clusters in rainbow
Several methods of creating thread classes
Principle, technology and implementation scheme of data consistency in distributed database
Global and Chinese market of protection circuit modules 2022-2028: Research Report on technology, participants, trends, market size and share
Copier le matériel de conseils de bébé ne peut pas être vide, comment résoudre?
[une question par jour pendant l'été] course luogu p1568
Global and Chinese market of mainboard 2022-2028: Research Report on technology, participants, trends, market size and share
在Rainbond中一键部署高可用 EMQX 集群
Character input stream and character output stream
Flutter 实现每次进来界面都刷新数据
Some common commands of podman
Use of STM32 expansion board temperature sensor and temperature humidity sensor
Solution: thread 1:[< *> setvalue:forundefined key]: this class is not key value coding compliant for the key*
How to start learning editing? Detailed analysis of zero basis
【暑期每日一题】洛谷 P7222 [RC-04] 信息学竞赛
Global and Chinese market of high-end home theater 2022-2028: Research Report on technology, participants, trends, market size and share
Flutter can refresh data every time the interface comes in
Global and Chinese markets of Ethernet communication modules 2022-2028: Research Report on technology, participants, trends, market size and share