【问题标题】:CSS triangle filling for a progress bar进度条的 CSS 三角形填充
【发布时间】:2018-05-16 23:56:49
【问题描述】:

我实际上用谷歌搜索并搜索了一些信息,但找不到。

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

JSFiddle

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

【问题讨论】:

  • 三角形的外侧会是块色吗?
  • @jbutler483 抱歉.. 块色是什么意思?
  • 你的背景会是纯色吗(即在你的三角形之外)?
  • @jbutler483 其实并不重要。我需要类似于进度条或类似于此的东西。有什么方法可以创建像三角形的进度条?
  • Yes, there is。使用这个想法将是我的建议......

标签: html css svg css-shapes


【解决方案1】:

为了制作三角形,我将使用两个伪元素将其从方形 div 中“剪掉”。然后,对于嵌套的 div,使用绝对定位允许您将其“填充”到某个值(通过以 % 为单位设置 .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>

【讨论】:

  • 不错,但仅使用一个元素的伪元素几乎可以实现
  • @TimKrul:是的,但是在操纵“数量”时,操纵“真实”元素比操纵有限的伪元素要明智得多……
  • 我怎么知道你会有这个问题的答案!哈哈,干得好。
  • @Ruddy:我不知道:P
【解决方案2】:

可以这样吗?

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

小提琴:http://jsfiddle.net/bkaxzLnu/3/

【讨论】:

    【解决方案3】:

    这是另一个 CSS ONLYNO-BORDERSNO 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>

    【讨论】:

      猜你喜欢
      • 2014-01-01
      • 2019-05-19
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-12
      相关资源
      最近更新 更多