2016-03-01 45 views
1

這更是一個問題CSS的適合的圓形進度條。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

您是否嘗試過加入 「最大寬度:80vw」 的.progress_chart? – Jacob

回答

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

+0

謝謝!這工作完美。在另一張紙上,進度圖看起來非常像素化。無論如何解決這個問題? (OP中的附加屏幕截圖) – Zhen

+1

這是因爲css被用於操縱畫布的W和H,這會拉伸畫布內的像素。爲了避免這種情況,您需要直接使用畫布的「高度」和「寬度」屬性。在你的情況下,js更改會增加畫布寬度以匹配當前視圖的端口大小。 [見編輯JS中](http://codepen.io/anon/pen/mPJyRP?editors=0010) ::::::::::::::::::::::: :::::::::::::::::::: VAR選項= { .... 尺寸:el.getAttribute( '數據大小')|| (screen.width - 5), lineWidth:el.getAttribute('data-line')|| 50, .... } :::::::::::::::::: – CreativeCreate

+0

這是偉大的!作品。非常感謝您的幫助。 – Zhen