CSS:如何根据屏幕大小(移动)调整圆形进度条

时间:2016-03-01 02:06:24

标签: css coffeescript progress-bar pug

这更像是一个CSS问题。

我已经从这个link获得了创建循环进度条的代码。我也粘贴了下面的代码。

目前,当我使用代码时,它会创建一个固定位置和大小的循环进度条。即屏幕尺寸不会扩大或缩小。

enter image description here

问题: 如何更新CSS以使其允许圆形条的大小并适合屏幕的大小?因为,圆形条应适合不同的移动屏幕尺寸。

enter image description here

创建循环进度条的代码(Coffescript)

el = document.getElementById('graph')
# get canvas
options = 
  percent: el.getAttribute('data-percent')
  size: el.getAttribute('data-size') or 220
  lineWidth: el.getAttribute('data-line') or 20
  rotate: el.getAttribute('data-rotate') or 0
canvas = document.createElement('canvas')
span = document.createElement('span')
span.textContent = options.percent + '%'
if typeof G_vmlCanvasManager != 'undefined'
 G_vmlCanvasManager.initElement canvas
ctx = canvas.getContext('2d')
canvas.width = canvas.height = options.size
el.appendChild span
el.appendChild canvas
ctx.translate options.size / 2, options.size / 2
# change center
ctx.rotate (-1 / 2 + options.rotate / 180) * Math.PI
# rotate -90 deg
#imd = ctx.getImageData(0, 0, 240, 240);
radius = (options.size - (options.lineWidth)) / 2

drawCircle = (color, lineWidth, percent) ->
  percent = Math.min(Math.max(0, percent or 1), 1)
  ctx.beginPath()
  ctx.arc 0, 0, radius, 0, Math.PI * 2 * percent, false
  ctx.strokeStyle = color
  ctx.lineCap = 'round'
  # butt, round or square
  ctx.lineWidth = lineWidth
  ctx.stroke()
  return

drawCircle '#efefef', options.lineWidth, 100 / 100
drawCircle '#555555', options.lineWidth, options.percent / 100

CSS

.progress_chart {
  position:relative;
  margin: 80px;
  width: 220px; height: 220px;
  canvas {
    display: block;
    position:absolute;
    top:0;
    left:0;
  }
  span {
    color:#555;
    display:block;
    line-height:220px;
    text-align:center;
    width:220px;
    font-family:sans-serif;
    font-size:40px;
    font-weight:100;
    margin-left:5px;
  }

  input {
    width: 200px;
  }  
}

JADE代码

.progress_chart
  #graph.chart(data-percent='14')

更新

像素图的屏幕截图 enter image description here

1 个答案:

答案 0 :(得分:1)

以下使用css3视口功能单元vw, vh, vmin, vmax来解决此问题。基本上是html元素' (canvas, span, div)高度将根据视口的宽度和宽度进行分配。高度。 See detail explanation

通过使用vmin(视口最小边的大小),我们可以调整适当的高度/宽度以匹配屏幕尺寸。

  div {
  /* take the viewport Width & Height */
  width: 100vw;
  height: 100vh;
  /* horizontal centers content */
  text-align: center;
  /* vertical centers content */
  display: table-cell;
  vertical-align: middle;
}

canvas {
  width: auto;
  /* 90% of smallest-side */
  height: 90vmin;
}

span {
  position: absolute;
  /* 20% of smallest-side */
  font-size: 20vmin;
  /* 90% of smallest-side */
  line-height: 90vmin;
  /* 90% of smallest-side */
  width: 90vmin;
}

See demo