首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建具有固定梯度的渐变进度条

如何创建具有固定梯度的渐变进度条
EN

Stack Overflow用户
提问于 2021-04-05 11:13:37
回答 3查看 6.4K关注 0票数 2

我对HTML和CSS相当陌生。我想要创建一个进度条,它有一个渐变的背景,它掩盖了整个元素,但只在条形图本身上可见。到目前为止,我只得到了1下面显示的结果。我试图通过操纵其他背景属性(如背景-附件)来实现第二幅图像中显示的结果,但是梯度本身不再适合。我还尝试给该条的父母一个渐变背景,只需在剩余的空间上方画一个白色的div,但这个解决方案禁止我向条形图本身添加边界半径。任何帮助都是非常感谢的。干杯!

我想要实现的

到目前为止我得到了什么

代码语言:javascript
复制
.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%;
}
代码语言:javascript
复制
<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>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-04-05 11:27:54

使用具有动态宽度的掩码的解决方案:

代码语言:javascript
复制
.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 */
}
代码语言:javascript
复制
<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>

票数 4
EN

Stack Overflow用户

发布于 2021-04-05 11:16:12

保持梯度宽度与容器宽度相同。

代码语言:javascript
复制
.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%;
}
代码语言:javascript
复制
<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>

票数 1
EN

Stack Overflow用户

发布于 2021-04-05 11:22:39

代码语言:javascript
复制
#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%;
  }
}
代码语言:javascript
复制
<div id="container">
        <div class="child progress"></div>
        <div class="child shrinker timelapse"></div>
</div>

工频

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66952087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档