使用边框半径绘制圆圈

时间:2012-07-12 15:43:47

标签: jquery css css3

我正在尝试使用边框半径转换一些div。 我几乎获得它,但有时div看起来像'鸡蛋'哈哈 这是我的css:

#div{   /*div central*/
    position:absolute;
    top:50%;
    margin-top: -20%;
    left:50%;
    margin-left: -20%;
    background: #00A8D9;
    width: 40%;
    height: 40%;
    -moz-border-radius: 50%;
    -webkit-border-radius: 50%;
    border-radius: 50%;
    border:3px solid #000;
}
#divSupIzq,#divSupDer,#divInfIzq,#divInfDer{    /*DIVs: left-top , right-top, left-bottom, right-bottom*/
    background: #ddd;
    width: 20%;
    height: 20%;
    -moz-border-radius: 50%;
    -webkit-border-radius: 50%;
    border-radius: 50%;
    border:3px solid #fff;
    position:absolute;
}
#divSupIzq{  /*Div left-top*/
    top:15%;
    left:10%;
}
#divSupDer{ /*Div right-top*/
    top:15%;
    right:10%;
}
#divInfIzq{ /*Div left-bottom*/
    bottom:15%;
    left:10%;
}
#divInfDer{ /*Div right-bottom*/
    bottom:15%;
    right:10%;
}

在html中,我使用javascript / jQuery来更改每个div的内容(基本上是div中的文本:左上角,右上角,左下角,右下角;以及中央div中的数字)取决于每个div的大小:

$('#div').resize(function(height){
                    var fuente = $(this).height()/2;
                    var margen = (fuente / 2)-5;
                    $('.contenido').css('font-size',fuente+'px');
                    $('.contenido').css('margin-top',margen+'px');  
                });

这是我在Chrome的Ripple扩展中看到的: https://www.dropbox.com/s/k71kz5lahfolw95/screen.JPG

你可以帮助我,以便div总是圈,而不是鸡蛋? 提前谢谢,丹尼尔

3 个答案:

答案 0 :(得分:9)

绘制圆圈:

<强> HTML

<div id="circle"></div>

<强> CSS

#circle {
    width: 100px;
    height: 100px;
    background: red;
    -moz-border-radius: 50px;
    -webkit-border-radius: 50px;
    border-radius: 50px;
}

Here是上述的小提琴。

固定宽度和高度:http://jsfiddle.net/eC3jq/1/

circle中包含

div,以便%宽度和高度正常工作:http://jsfiddle.net/eC3jq/2/

来源:CSS-Tricks

答案 1 :(得分:1)

JQuery的

这也很有用,如果您将所有这些代码复制到您的网站,它将起作用。或者您可以看到演示:

https://jsfiddle.net/whLctrp4/

调用JQuery函数的HTML代码:

toString()

包含JQuery

<!DOCTYPE html>
<html>
    <head>
        <title>Page Title</title>
        <link rel="stylesheet" type="text/css" href="style.css">

    </head>
    <body>

    <div class="pies">
    </div>   

drawPie函数 - 作为参数id / class html属性,大小(以px为单位),填充百分比和馅饼颜色

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

对于show的循环,它是如何工作的:

    <script>

    function drawPie (id, size, percent, color) {                               
        var sizeString = "" + size + "px";                      
        var grad = 360/100*percent+90;      
        console.log(grad);
        var pie = $("<span></span>");

        pie.css({"width": sizeString, 
            "height": sizeString,               
            "display": "block",
            "border-radius": "50%",
            "background-color": color,                          
            "float": "left",
            "margin": "5px"             
        });         

        if(percent <= 50){
            pie.css({"background-image": "linear-gradient("+ grad + "deg, transparent 50%, white 50%), linear-gradient(90deg, white 50%, transparent 50%)"});
        } else if (percent >= 100) {
            pie.css({"background-image": "none"});
        } else {                                
            pie.css({"background-image": "linear-gradient("+ (grad+180) + "deg, transparent 50%, "+color+" 50%), linear-gradient(+90deg, white 50%, transparent 50%)"});                
        }

        $(id).append(pie);
    }

答案 2 :(得分:0)

工作演示:http://jsfiddle.net/XtTkG/3/

该演示通过使用窗口对象的resize事件而不是div本身来工作。在每个调整大小时,我们调整div及其边界半径,以便它呈现为一个完美的圆(即width = height和radius = width / 2)

代码:

$(window).resize(function(height) {
    var div = $('#div');
    var width = $('body').width() * 0.4;
    var radius = width/2;
    width += 'px';
    radius += 'px';
    div.css({
        width: width, height: width,
        '-moz-border-radius' : radius,
        '-webkit-border-radius' : radius,
        'border-radius' : radius
    });

    // rest of your code for font-size setting etc..
});

$(window).resize();