每天CookBook之JavaScript-018

  • 获取DIV的高度和宽度并在DIV中画圆
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>018</title>
    <style type="text/css">
    #elem
    {
        width: 600px;
        height: 400px;
        border: 1px solid black;
    }
    </style>
    <script type="text/javascript">
    window.onload = window.onresize = function(){
        var box = document.getElementById('elem');
        var style = window.getComputedStyle(box,null);
        var height = parseInt(style.getPropertyValue("height"));
        var width = parseInt(style.getPropertyValue("width"));
        var x = width/2;
        var y = height/2;

        var circleRadius = Math.min(width, height)/2;
        var circ = document.getElementById("circ");
        circ.setAttribute("r", circleRadius);
        circ.setAttribute("cx", x);
        circ.setAttribute("cy", y);
    }
    </script>
</head>
<body>
    <div id="elem">
        <svg width="100%" height="100%">
            <circle id="circ" width="10" height="10" r="10" fill="red"></circle>
        </svg>
    </div>
</body>
</html>
posted @ 2016-07-09 11:14  4Thing  阅读(72)  评论(0编辑  收藏  举报