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>

转载于:https://www.cnblogs.com/sky-zhao/p/5054074.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值