如何制作固定渐变的渐变进度条
How to create a gradient progress bar with a fixed gradient
我对 HTML 和 CSS 比较陌生。我想创建一个进度条,它有一个渐变背景,它掩盖了整个元素,但只在进度条本身可见。到目前为止,我只设法得到如下所示的结果 1。我试图通过操纵其他背景属性(例如背景附件)来实现第二张图片中显示的结果,但是渐变本身不再适合。我还尝试为条形图的父级提供渐变背景,并在剩余的 space 上方简单地绘制一个白色 div,但该解决方案禁止我向条形图本身添加边框半径。非常感谢任何帮助。干杯!
What I'm trying to achieve
What I've got so far
.progress-bar-container {
width: 500px;
height: 50px;
margin: 50px 0px;
background: black;
}
.progress-bar-indicator {
height: 100%;
background-image: linear-gradient(to right, red, green, blue);
border-radius: 25px;
}
#indicator-1 {
width: 80%;
}
#indicator-2 {
width: 50%;
}
#indicator-3 {
width: 20%;
}
<div class="progress-bar-container">
<div class="progress-bar-indicator" id="indicator-1"></div>
</div>
<div class="progress-bar-container">
<div class="progress-bar-indicator" id="indicator-2"></div>
</div>
<div class="progress-bar-container">
<div class="progress-bar-indicator" id="indicator-3"></div>
</div>
保持渐变宽度与容器宽度相同。
.progress-bar-container {
width: 500px;
height: 50px;
margin: 50px 0px;
background: black;
}
.progress-bar-indicator {
height: 100%;
background-image: linear-gradient(to right, red, green, blue);
/* ↓ same as container width */
background-size: 500px 100%;
border-radius: 25px;
}
#indicator-1 {
width: 80%;
}
#indicator-2 {
width: 50%;
}
#indicator-3 {
width: 20%;
}
<div class="progress-bar-container">
<div class="progress-bar-indicator" id="indicator-1"></div>
</div>
<div class="progress-bar-container">
<div class="progress-bar-indicator" id="indicator-2"></div>
</div>
<div class="progress-bar-container">
<div class="progress-bar-indicator" id="indicator-3"></div>
</div>
#container {
width: 800px;
height: 60px;
margin: 0 auto;
position: relative;
top: 50px;
transform: translateY(-50%);
border-radius: 35px;
overflow: hidden;
}
.child {
width: 100%;
height: 100%;
}
.progress {
color: white;
text-align: center;
line-height: 75px;
font-size: 35px;
font-family: "Segoe UI";
animation-direction: reverse;
background: #e5405e;
/* Old browsers */
background: -moz-linear-gradient(left, #e5405e 0%, #ffdb3a 25%, #3fffa2 50%, #3fffa2 50%, #1a9be0 73%, #ba68ed 100%);
/* FF3.6-15 */
background: -webkit-linear-gradient(left, #e5405e 0%, #ffdb3a 25%, #3fffa2 50%, #3fffa2 50%, #1a9be0 73%, #ba68ed 100%);
/* Chrome10-25,Safari5.1-6 */
background: linear-gradient(to right, #e5405e 0%, #ffdb3a 25%, #3fffa2 50%, #3fffa2 50%, #1a9be0 73%, #ba68ed 100%);
}
.shrinker {
background-color: black;
position: absolute;
top: 0;
right: 0;
width: 100%;
}
.timelapse {
animation-name: timelapse;
animation-fill-mode: forwards;
animation-duration: 5s;
animation-timing-function: cubic-bezier(.86, .05, .4, .96);
}
@keyframes timelapse {
0% {
width: 100%;
}
100% {
width: 0%;
}
}
<div id="container">
<div class="child progress"></div>
<div class="child shrinker timelapse"></div>
</div>
使用动态宽度的遮罩解决方案:
.progress-bar-container {
height: 50px;
margin: 50px 0px;
background: black;
position:relative; /* relative here */
}
.progress-bar-indicator {
height: 100%;
border-radius: 25px;
/* this will do the magic */
-webkit-mask:linear-gradient(#fff 0 0);
mask:linear-gradient(#fff 0 0);
}
.progress-bar-indicator::before {
content:"";
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
background-image: linear-gradient(to right, red, green, blue); /* your gradient here */
}
<div class="progress-bar-container">
<div class="progress-bar-indicator" style="width:80%"></div>
</div>
<div class="progress-bar-container">
<div class="progress-bar-indicator" style="width:50%"></div>
</div>
<div class="progress-bar-container">
<div class="progress-bar-indicator" style="width:30%"></div>
</div>
我对 HTML 和 CSS 比较陌生。我想创建一个进度条,它有一个渐变背景,它掩盖了整个元素,但只在进度条本身可见。到目前为止,我只设法得到如下所示的结果 1。我试图通过操纵其他背景属性(例如背景附件)来实现第二张图片中显示的结果,但是渐变本身不再适合。我还尝试为条形图的父级提供渐变背景,并在剩余的 space 上方简单地绘制一个白色 div,但该解决方案禁止我向条形图本身添加边框半径。非常感谢任何帮助。干杯!
What I'm trying to achieve
What I've got so far
.progress-bar-container {
width: 500px;
height: 50px;
margin: 50px 0px;
background: black;
}
.progress-bar-indicator {
height: 100%;
background-image: linear-gradient(to right, red, green, blue);
border-radius: 25px;
}
#indicator-1 {
width: 80%;
}
#indicator-2 {
width: 50%;
}
#indicator-3 {
width: 20%;
}
<div class="progress-bar-container">
<div class="progress-bar-indicator" id="indicator-1"></div>
</div>
<div class="progress-bar-container">
<div class="progress-bar-indicator" id="indicator-2"></div>
</div>
<div class="progress-bar-container">
<div class="progress-bar-indicator" id="indicator-3"></div>
</div>
保持渐变宽度与容器宽度相同。
.progress-bar-container {
width: 500px;
height: 50px;
margin: 50px 0px;
background: black;
}
.progress-bar-indicator {
height: 100%;
background-image: linear-gradient(to right, red, green, blue);
/* ↓ same as container width */
background-size: 500px 100%;
border-radius: 25px;
}
#indicator-1 {
width: 80%;
}
#indicator-2 {
width: 50%;
}
#indicator-3 {
width: 20%;
}
<div class="progress-bar-container">
<div class="progress-bar-indicator" id="indicator-1"></div>
</div>
<div class="progress-bar-container">
<div class="progress-bar-indicator" id="indicator-2"></div>
</div>
<div class="progress-bar-container">
<div class="progress-bar-indicator" id="indicator-3"></div>
</div>
#container {
width: 800px;
height: 60px;
margin: 0 auto;
position: relative;
top: 50px;
transform: translateY(-50%);
border-radius: 35px;
overflow: hidden;
}
.child {
width: 100%;
height: 100%;
}
.progress {
color: white;
text-align: center;
line-height: 75px;
font-size: 35px;
font-family: "Segoe UI";
animation-direction: reverse;
background: #e5405e;
/* Old browsers */
background: -moz-linear-gradient(left, #e5405e 0%, #ffdb3a 25%, #3fffa2 50%, #3fffa2 50%, #1a9be0 73%, #ba68ed 100%);
/* FF3.6-15 */
background: -webkit-linear-gradient(left, #e5405e 0%, #ffdb3a 25%, #3fffa2 50%, #3fffa2 50%, #1a9be0 73%, #ba68ed 100%);
/* Chrome10-25,Safari5.1-6 */
background: linear-gradient(to right, #e5405e 0%, #ffdb3a 25%, #3fffa2 50%, #3fffa2 50%, #1a9be0 73%, #ba68ed 100%);
}
.shrinker {
background-color: black;
position: absolute;
top: 0;
right: 0;
width: 100%;
}
.timelapse {
animation-name: timelapse;
animation-fill-mode: forwards;
animation-duration: 5s;
animation-timing-function: cubic-bezier(.86, .05, .4, .96);
}
@keyframes timelapse {
0% {
width: 100%;
}
100% {
width: 0%;
}
}
<div id="container">
<div class="child progress"></div>
<div class="child shrinker timelapse"></div>
</div>
使用动态宽度的遮罩解决方案:
.progress-bar-container {
height: 50px;
margin: 50px 0px;
background: black;
position:relative; /* relative here */
}
.progress-bar-indicator {
height: 100%;
border-radius: 25px;
/* this will do the magic */
-webkit-mask:linear-gradient(#fff 0 0);
mask:linear-gradient(#fff 0 0);
}
.progress-bar-indicator::before {
content:"";
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
background-image: linear-gradient(to right, red, green, blue); /* your gradient here */
}
<div class="progress-bar-container">
<div class="progress-bar-indicator" style="width:80%"></div>
</div>
<div class="progress-bar-container">
<div class="progress-bar-indicator" style="width:50%"></div>
</div>
<div class="progress-bar-container">
<div class="progress-bar-indicator" style="width:30%"></div>
</div>