jquery - Bootstrap 进度条,渐变颜色按事件宽度按比例显示

标签 jquery css twitter-bootstrap-3 progress-bar

我需要制作一个由渐变颜色填充的 Bootstrap 进度条(比如说从红色到绿色)。我的 CSS 目前看起来像这样:

.progress-lf {
    position: relative;
    height: 31px;
    background-color: rgba(51, 51, 51, 0.4)
}

.progress-lf span {
    position: absolute;
    display: block;
    font-weight: bold;
    color: #d2d2d2;
    width: 100%;
    top:6px;
}

.progress-lf .gradient {
    background-color: transparent;
    background-image: -ms-linear-gradient(left, #E34747 0%, #5FB365 100%);
    background-image: -moz-linear-gradient(left, #E34747 0%, #5FB365 100%);
    background-image: -o-linear-gradient(left, #E34747 0%, #5FB365 100%);
    background-image: -webkit-gradient(linear, left top, right top, color-stop(0, #E34747), color-stop(100, #5FB365));
    background-image: -webkit-linear-gradient(left, #E34747 0%, #5FB365 100%);
    background-image: linear-gradient(to right, #E34747 0%, #5FB365 100%);
}

和它一起去的 HTML 是这样的:

    <div class="progress progress-lf">
            <div class="progress-bar gradient" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo mt_rand(0,100);?>%;">
                    <span>60% Complete</span>
            </div>
    </div>

这会显示渐变,但对于上面的示例 (60%),它会显示整个 60% 活跃区域的渐变色谱。我需要对此进行更改,以便(例如)对于 60%,仅显示 60% 的渐变色谱。

有人对如何实现这一目标有任何想法吗?我更喜欢纯 CSS 解决方案,但如果需要 jQuery 来实现这一点,那也可以。

最佳答案

为了让您动态更改“数量”,我建议使用 jquery(或 vanilla js,以首选者为准)来调整进度条。

为了完成进度条的值,我使用了data-attribute,还有文本(所以你只需要改变一个地方)。


这意味着你所要做的就是改变

data-amount 

属性值介于 0 和 100% 之间。


演示

 $(document).ready(function () {
    var dataval = parseInt($('.progress').attr("data-amount"));
    if (dataval < 100) {
        $('.progress .amount').css("width", 100 - dataval + "%");
    }

  /*FOR DEMO ONLY*/
    $('#increase').click(function () {
        modifyProgressVal(1);
    });
    $('#decrease').click(function () {
        modifyProgressVal(-1);
    });
    function modifyProgressVal(type) {
        dataval = parseInt($('.progress').attr("data-amount"));
        if (type == 1) dataval = Math.min(100,dataval + 10)
        else if (type == -1) dataval = Math.max(0,dataval - 10);
        $('.progress .amount').css("width", 100 - dataval + "%");
        $('.progress').attr("data-amount", dataval);
    }
});
.progress {
  position: relative;
  height: 31px;
  background: rgb(255, 0, 0);
  background: -moz-linear-gradient(left, rgba(255, 0, 0, 1) 0%, rgba(0, 255, 0, 1) 100%);
  background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 0, 0, 1)), color-stop(100%, rgba(0, 255, 0, 1)));
  background: -webkit-linear-gradient(left, rgba(255, 0, 0, 1) 0%, rgba(0, 255, 0, 1) 100%);
  background: -o-linear-gradient(left, rgba(255, 0, 0, 1) 0%, rgba(0, 255, 0, 1) 100%);
  background: -ms-linear-gradient(left, rgba(255, 0, 0, 1) 0%, rgba(0, 255, 0, 1) 100%);
  background: linear-gradient(to right, rgba(255, 0, 0, 1) 0%, rgba(0, 255, 0, 1) 100%);
  filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#ff0000', endColorstr='#00ff00', GradientType=1);
}
.amount {
  position: absolute;
  top: 0;
  right: 0;
  height: 100%;
  transition: all 0.8s;
  background: gray;
  width: 0;
}
.progress:before {
  content: attr(data-amount)"% Complete";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 10;
  text-align: center;
  line-height: 31px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="progress" data-amount="80">
  <div class="amount"></div>
</div>


<!--FOR DEMO ONLY-->

<button id="increase">Increase by 10</button>
<button id="decrease">Decrease by 10</button>

这实际上只使用了两个元素,因此在性能方面应该相当不错。

注意

这个答案中似乎使用了相当多的 jQuery;这是由于 DEMO 而不是实际使用。

关于jquery - Bootstrap 进度条,渐变颜色按事件宽度按比例显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29519868/

相关文章:

jquery - 如何在左侧div上创建一个尖三 Angular 形

jquery - 如何在使用jquery添加元素后应用CSS

html - 响应式网页设计,缩放图像;节省带宽?

css - 在 WordPress TwentyTen 主题的子项中覆盖 CSS 未生效

css - 在 wkhtmltopdf 中使用自定义字体

twitter-bootstrap-3 - 使用 React Bootstrap : accordion won't work

css - Bootstrap 的最少 LESS 文件(最新)

javascript - 如何使用javascript onclick创建表单元素输入框,标签

jquery - 动态更新谷歌元标签是否可取?如果通过 ajax 请求更新,谷歌是否会更新描述和标题等元标记?

javascript - 如何将 javascript onSubmit 与 jQuery 提交相匹配?