每天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>

转载于:https://www.cnblogs.com/4thing/p/5655491.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值