【问题标题】:How to make progress bar with dotted lines?如何用虚线制作进度条?
【发布时间】:2017-06-03 10:15:09
【问题描述】:

也许你已经看过这样的进度条了?例如分享链接。有什么想法吗?谢谢! Image progress bar

【问题讨论】:

标签: javascript css sass progress-bar


【解决方案1】:

我认为画布不是最好的解决方案...

您可以使用 SVG 轻松创建它:

Some Lines 作为示例,修改它以解决您的问题:

      var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
      svg.setAttribute('width', '200');
      svg.setAttribute('height', '200');
      svg.setAttribute("viewBox", "0 0 200 200"); 
      svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink", "http://www.w3.org/1999/xlink");

    var myLine=document.createElementNS("http://www.w3.org/2000/svg", "line");
      with(myLine){
        setAttribute("x1", "100");
        setAttribute("y1", "5");
        setAttribute("x2", "100");
        setAttribute("y2", "15");
        setAttribute("stroke", "#ccc");
        setAttribute("stroke-width", "2");
        setAttribute("transform", "rotate(0,100,100)");

      }
    var myNodes=[];
    var els=100;
    var step = 360/els;
    for (var i=0;i<els;i++){
      myNodes[i]=myLine.cloneNode(true);
      myNodes[i].setAttribute("transform", "rotate("+i*step+",100,100)");
      svg.appendChild(myNodes[i]);
    }

    //make 30% red;
    var percent=30;
    for (i=0;i<=percent;i++){
      myNodes[i].setAttribute("stroke", "#f00");
    }
    document.body.appendChild(svg);

看到一个盖子fiddle

【讨论】:

    【解决方案2】:

    如果你想要一个简单的进度条,你可以使用 HTML5 的 &lt;progress&gt; 元素 (https://developer.mozilla.org/en/docs/Web/HTML/Element/progress),它似乎得到了很好的支持:http://caniuse.com/#feat=progress

    然后您可以在其上应用自定义样式,以便在进度增加时可以看到小点。

    【讨论】:

      【解决方案3】:

      HTML5 Canvas API 是处理这类事情的绝佳纯 JS 解决方案。

      看看这个 - https://codepen.io/JanAmbrozic/pen/ZpVvXN

      var progressCircle = {
        init: function(width, height, lineWidth, color) {
          var el = document.getElementById('progressCircle');    
          this.canvas = document.getElementById('progCanvas');
          this.span = document.getElementById('progSpan');
          this.span.textContent = '0%';    
          this.ctx = this.canvas.getContext('2d');
          this.canvas.width = width;
          this.canvas.height = height;
          this.ctx.lineWidth = lineWidth;
          this.ctx.lineCap = 'round';
          this.ctx.translate(width / 2, height / 2);
          this.ctx.rotate(-90*Math.PI/180);    
          this.radius = (width - lineWidth) / 2;
          this.drawCircle("#edc79b", 100);
        },
      
        drawCircle : function(color, percent) {    
              percent = percent/100;
              this.ctx.beginPath();
              this.ctx.arc(0, 0, this.radius, 0, Math.PI * 2 * percent, false);
          this.ctx.strokeStyle = color;
              this.ctx.stroke();
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-26
        • 1970-01-01
        • 2012-02-07
        • 2021-08-19
        • 1970-01-01
        • 2022-10-16
        相关资源
        最近更新 更多