Javascript笔记-在div标签中填充svg圆形

页面加载事件:window.onload

窗口大小改变事件:window.onresize

getComputedStyle:获取当前元素所有最终使用的CSS属性值,返回的是一个CSS样式声明对象:(仅能读取样式);

element.style可读可写样式。

getPropertyValue:获取CCS样式声明对象上的属性值;

setAttribute:设置属性

svg:可缩放矢量图形

circel标签创建一个圆;

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Using Math method to fit a circle</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 @ 2015-12-17 14:32  sky.zhao  阅读(947)  评论(0编辑  收藏  举报