【问题标题】:Display percentage in Jquery progressbar在 Jquery 进度条中显示百分比
【发布时间】:2017-07-27 09:35:05
【问题描述】:

我的页面中有一个动态更新的进度条,我想在其中显示百分比,但我不知道怎么做。这是我页面上的进度条:

<div class="progress progress-striped active"> 
   <div id="prog" class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">
      <span id="progress-value"></span>
   </div> 
</div>

这是我更新进度条的 jQuery 代码:

$.ajax({    
    xhr: function () {
        var xhr = new window.XMLHttpRequest();
        xhr.upload.addEventListener("progress", function (evt) {
            if (evt.lengthComputable) {
                var pct = evt.loaded / evt.total;

                $("#prog").css({ width: pct * 100 + '%' });
                $('#progress-value').html(pct * 100);

            }
        }, false);

        xhr.addEventListener("progress", function (evt) {
            if (evt.lengthComputable) {
                var pct = evt.loaded / evt.total;

                $("#prog").css({ width: pct * 100 + '%' });
                $('#progress-value').html(pct*100);
                if (pct === 1) {
                    $('#prog').addClass('hide');
                }
            }
        }, false);
        return xhr;
    },

它会更新进度条,因为我在数据加载期间看到它在进行,但它不显示百分比。我做错了什么?

【问题讨论】:

  • $('#prog').removeClass('hide');在 xhr 的开头?
  • 是的,我有几个按钮,每个按钮都调用 ajax 请求,在加载结束时我隐藏进度条以显示数据。当我调用一个按钮时,我删除了 Hide 类,因为我想再次显示进度条,它可以工作

标签: jquery ajax twitter-bootstrap


【解决方案1】:

我要做的是在元素上使用data- 属性(在这种情况下我将使用data-progress),以及CSS 中该元素的content: 属性。

例如JQuery:

$.ajax({    
xhr: function () {
    var xhr = new window.XMLHttpRequest();
    xhr.upload.addEventListener("progress", function (evt) {
        if (evt.lengthComputable) {
            var pct = evt.loaded / evt.total;

            $("#prog").css({ width: pct * 100 + '%' })
                      .attr('data-progress', pct * 100 + '%');

        }
    }, false);

    xhr.addEventListener("progress", function (evt) {
        if (evt.lengthComputable) {
            var pct = evt.loaded / evt.total;

            $("#prog").css({ width: pct * 100 + '%' })
                      .attr('data-progress', pct * 100 + '%');
            if (pct === 1) {
                $('#prog').addClass('hide');
            }
        }
    }, false);
    return xhr;
},

所以在代码中,你会看到它在更新进度条的物理宽度的同时,也更新了数据属性。当然,CSS 会自动更新为新值。

在 CSS 中,它会是:

#prog[data-progress] {
    position: relative;
}
#prog[data-progress]:before {
    position: absolute;
    content: attr(data-progress);
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%); /* this sets the text to be exactly in the middle of the parent element */
}

【讨论】:

  • 谢谢,我试过了,还是不显示百分比
  • 如果出现 :before 伪元素,您是否检查过开发者控制台?您是否尝试过使用进度条的 z-index 和伪元素?
  • 是的,它创建了 :before 元素,我尝试为 span 元素分配一个很大的 z-index 值,但 span 中的文本仍然不可见。
  • 这个解决方案是为了替换 &lt;span id="progress-value"&gt; 标签的需要。从 HTML 页面中删除它,因为 :before 伪元素取代了对 span 标记的需求。
  • 好的,我解决了,在 Chrome 开发者工具中我注意到 .progress 类添加了一个 text-indent: -9999px; 属性,因此它没有显示百分比。我在&lt;div id="prog"&gt; 中添加了text-indent: initial; 样式属性。我用$("#prog").css({ width: pct * 100 + '%' }).text(pct * 100 + '%'); 更新百分比并显示百分比值
猜你喜欢
  • 1970-01-01
  • 2011-01-12
  • 1970-01-01
  • 2017-04-02
  • 1970-01-01
  • 1970-01-01
  • 2021-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多