我有一个从外部资源获得的球的图像,我需要获取图像的大小(高度和宽度)以进行进一步的计算,但我尝试的内容显示0(但实际上它是40px)
下面是整个代码:
<!DOCTYPE html>
<html dir="ltr">
<head>
<meta charset="utf-8">
<title>Ball to center</title>
<style>
#field {
width: 200px;
border: 10px groove black;
background-color: #00FF00;
position: relative;
}
#ball {
position: absolute;
}
</style>
</head>
<body>
<div id="field">
<img src="https://en.js.cx/clipart/ball.svg" id="ball"> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
</div>
<script type="text/javascript">
let field = document.getElementById('field');
let ball = document.getElementById('ball');
console.log(`natural height and width: ${ball.naturalHeight} - ${ball.naturalWidth}`);
console.log(`client height and width: ${ball.clientHeight} - ${ball.clientWidth}`);
</script>
</body>
</html>
慕码人2483693
素胚勾勒不出你
相关分类