2016-07-07 128 views
0

我在想如何根据给定的程度从圆圈的中心向外画出一条直线。 (我最终希望每30度一行产生12条总线)用CSS画出圆圈的半径线

下面是一个类似于我想要实现的内容的图片。

enter image description here

CSS的圆我目前:

.circle-container{ 
    display: block; 
    position: absolute; 
    top: 15.5%; 
    left: 14.5%; 
    background: #fff; 
    width: 11.5em; 
    height: 11.5em; 
    border-radius: 50%; 
    border-style: solid; 
    border-width: thin; 
    border-color: #bfbfbf; 
    margin: 0em; 
} 

我真的不知道我应该开始。

+1

我会使用SVG ......否则你会unsemantic元素只是为了造型的目的很多告终。 –

+2

或画布元素 – j08691

回答

4

我会用帆布或者SVG ......否则,最终会出现许多仅用于样式目的的非语义元素。

然而,一个简单的例子

.circle-container { 
 
    display: block; 
 
    position: absolute; 
 
    top: 15.5%; 
 
    left: 14.5%; 
 
    background: #fff; 
 
    width: 11.5em; 
 
    height: 11.5em; 
 
    border-radius: 50%; 
 
    border-style: solid; 
 
    border-width: thin; 
 
    border-color: #bfbfbf; 
 
    margin: 0em; 
 
} 
 
.radius { 
 
    position: absolute; 
 
    width: 50%; 
 
    height: 3px; 
 
    left: 50%; 
 
    top: 50%; 
 
    background: red; 
 
    transform-origin: left center; 
 
} 
 
.two { 
 
    background: green; 
 
    transform: rotate(-30deg); 
 
}
<div class="circle-container"> 
 
    <div class="radius"></div> 
 
    <div class="radius two"></div> 
 
</div>

+0

感谢您的推荐! – ajkey94

1

可以通过将高度属性设置为0,宽度50%,这是直径的一半并创建边界创建的线。

要进行转换,您需要使用rotate属性以及transform-origin

这是一个简单的例子。

var radius = document.getElementById("radius"); 
 
var rotate = document.getElementById("rotate"); 
 

 
var rotation = 0; 
 

 
rotate.addEventListener("click", function() { 
 
    rotation -= 30; 
 
    radius.style.transform = "rotate(" + rotation + "deg)"; 
 
    radius.style.transformOrigin = "center right"; 
 
});
.circle-container{ 
 
    display: block; 
 
    position: absolute; 
 
    top: 15.5%; 
 
    left: 14.5%; 
 
    background: #fff; 
 
    width: 11.5em; 
 
    height: 11.5em; 
 
    border-radius: 50%; 
 
    border-style: solid; 
 
    border-width: thin; 
 
    border-color: #bfbfbf; 
 
    margin: 0em; 
 
} 
 

 
#radius { 
 
    height: 0px; 
 
    width: 50%; 
 
    border: 1px solid black; 
 
    top: 50%; 
 
    position: absolute; 
 
}
<div class="circle-container"> 
 
    <div id="radius"></div> 
 
</div> 
 

 
<button id="rotate">Rotate Radius</button>

至于为具有每隔30度线,让我们考虑一下。圆是360度,这意味着我们需要12行。在HTML中,这需要很多开销。为此,我们必须做很多DOM操作。为此,我会推荐Canvas,或者其中一个JavaScript画布库,如jcanvas

0

如果你想要超级灵活的东西,我会做这样的事情...你可以添加另一个带有ID的行,并给每个人一个单独的旋转,以获得你所有的角度。

.line{ 
    width: 1px; 
    height: 50%; 
    background: #000; 
    display: block; 
    position: absolute; 
    top: 0; 
    left: 50%; 
    transform-origin: bottom center; 
    -webkit-transform-origin: bottom center; 
    transform: rotate(45deg); 
    -webkit-transform: rotate(45deg); 
} 
.line:before{ 
    content: ''; 
    display: block; 
    position: absolute; 
    bottom: 0; 
    left: 0; 
    transform: translateX(-50%); 
    -webkit-transform: translateX(-50%); 
    width: 15px; 
    height: 15px; 
    border-radius: 100%; 
    background: #000; 
} 

<div class="circle-container"> 
    <div class="line"></div> 
</div> 
0

另一个解决办法是将克隆的线条,就像你说的,你需要12线

在这里看到:jsfiddle

JQ:

var degr = 0; 
var rotation = 0; 
//create a rotate function 
jQuery.fn.rotate = function(degrees) { 
    $(this).css({'transform' : 'rotate('+ degrees +'deg)'}); 
}; 
//clone the lines 
for(var i = 1; i < 5; i++){ 
    $(".line").clone().appendTo(".circle-container") 
} 


//rotate the lines 
$('.line').each(function() { 

degr += 30; 
$(this).rotate(degr); 
}); 

HTML:

<div class="circle-container"> 
    <div class="line"> 

    </div> 

</div> 

CSS补充说:

.circle-container:before { 
    position:absolute; 
    content:""; 
    width:30px; 
    height:30px; 
    background:#000; 
    left:0; 
    right:0; 
    margin:0 auto; 
    top:calc(50% - 15px); 
    border-radius:100%; 
} 
.circle-container .line { 
    position: absolute; 
    width: 50%; 
    height: 3px; 
    left: 50%; 
    top: 50%; 
    background: #000; 
    transform-origin: left center; 
    transform: rotate(0deg); 
}