CSS 进度条的三角形填充

CSS triangle filling for a progress bar

我确实用谷歌搜索了一些信息,但没找到。

我的目标是实现类似于进度条样式的东西,例如填充三角形内部。有什么办法吗?

JSFiddle

.angle {
    width: 0; 
    height: 0; 
    border-left: 75px solid transparent;
    border-right: 75px solid transparent;       
    border-bottom: 75px solid black;
}

可以这样吗?

.angle {
    position: relative;
    width: 0; 
    height: 0; 
    border-left: 100px solid transparent;
    border-right: 100px solid transparent;
    border-bottom: 100px solid blue;
}

.angle:after {
    position: absolute;
    content: "";
    top: 0;
    left: 50%;
    margin-left: -50px;
    width: 0; 
    height: 0; 
    border-left: 50px solid transparent;
    border-right: 50px solid transparent;
    border-bottom: 50px solid black;
}

fiddle: http://jsfiddle.net/bkaxzLnu/3/

为了制作三角形,我会使用两个伪元素来 'cut it out' 正方形 div。然后,对于嵌套的 div,使用绝对定位允许您将其 'fill' 设置为特定值(通过设置 .amount div 的高度,以 % 为单位)。

.amount {
  position: absolute;
  height: 0%;
  width: 100%;
  bottom: 0;
  left: 0;
  transition: all 1s;
  background: tomato;
}
.tri {
  position: relative;
  height: 200px;
  width: 200px;
  background: lightgray;
}
.tri:before,
.tri:after {
  content: "";
  position: absolute;
  border-top: 200px solid white;
  top: 0;
  z-index: 8;
}
.tri:before {
  border-left: 100px solid transparent;
  left: 50%;
}
.tri:after {
  border-right: 100px solid transparent;
  left: 0;
}
.tri:hover .amount {
  height: 100%;
}
<div class="tri">
  <div class="amount"></div>
</div>

这是另一个 CSS ONLY, NO-BORDERS, NO AFTER/BEFORE HACKS 选项:

您可以使用 clip-path。它允许您仅显示元素的一部分并隐藏其余部分。

所以你可以这样做:

  .amount {
    position: absolute;
    height: 100%;
    width: 0%;
    bottom: 0;
    left: 0;
    transition: all 1s;
    background: tomato;
  }

  .tri {
    position: relative;
    width: 500px;
    height: 50px;
    background: #ddd;

    /* triangle */
    clip-path: polygon( 100% 0%,100% 100%, 0% 100%);
  }
  .tri:hover .amount {
    width: 100%;
    background: chartreuse ;
  }
<div class="tri">
  <div class="amount"></div>
</div>