【发布时间】: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