【问题标题】:How to create a gradient progress bar with a fixed gradient如何创建具有固定渐变的渐变进度条
【发布时间】:2021-04-05 19:27:54
【问题描述】:

我对 HTML 和 CSS 比较陌生。我想创建一个具有渐变背景的进度条,它掩盖了整个元素,但仅在条本身上可见。到目前为止,我只设法得到下面1 所示的结果。我试图通过操纵其他背景属性(例如背景附件)来实现第二张图像中显示的结果,但是渐变本身不再适合。我还尝试为栏的父级提供渐变背景,并在剩余空间上方简单地绘制一个白色 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>

【问题讨论】:

    标签: html css progress-bar gradient


    【解决方案1】:

    使用可用于动态宽度的掩码的解决方案:

    .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>

    【讨论】:

    • 看起来很干净。谢谢!
    【解决方案2】:

    保持渐变宽度与容器宽度相同。

    .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>

    【讨论】:

    • 我得到答案的速度令人惊讶。非常感谢!
    【解决方案3】:

    #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>

    Working Fiddle

    【讨论】:

    • 感谢您的回答。我想我以前也偶然发现了这个解决方案。我的问题是为栏本身添加边框半径。
    猜你喜欢
    • 1970-01-01
    • 2017-02-09
    • 2021-01-21
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多