【问题标题】:CSS: How to fit a circular progress bar according to screen size (mobile)CSS:如何根据屏幕大小调整圆形进度条(移动)
【发布时间】:2016-06-13 07:26:13
【问题描述】:

这更像是一个 CSS 问题。

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

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

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

创建循环进度条的代码(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;
  }  
}

玉码

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

更新

像素化图的屏幕截图

【问题讨论】:

  • 您是否尝试在 .progress_chart 中添加“max-width: 80vw”?

标签: css coffeescript progress-bar pug


【解决方案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

【讨论】:

  • 谢谢!这完美地工作。在单独的说明中,进度图表看起来非常像素化。有没有办法解决这个问题? (附在OP中的截图)
  • 那是因为 css 被用来操纵画布的 W 和 H,这会拉伸画布内的像素。为避免这种情况,您需要直接使用画布的 heightwidth 属性。在您的情况下,以下 js 更改将增加画布宽度以匹配当前视口大小。 [查看 Js 中的编辑] (codepen.io/anon/pen/mPJyRP?editors=0010) ::::::::::::::::::::::::::::::::::::::: :::: var options = { .... size: el.getAttribute('data-size') || (screen.width - 5), lineWidth: el.getAttribute('data-line') || 50, .... }:::::::::::::::::::
  • 这太棒了!作品。非常感谢您在这方面的帮助。
猜你喜欢
  • 1970-01-01
  • 2017-05-26
  • 2015-07-16
  • 1970-01-01
  • 2017-01-19
  • 1970-01-01
  • 2013-11-25
  • 1970-01-01
相关资源
最近更新 更多