简体   繁体   English

getBoundingClientRect,内联SVG的高度值不正确

[英]getBoundingClientRect, incorrect height value for inline SVG

<body style="width:1000px;height:1000px;">
    <div id="d" style="display:inline-block;">
        <svg id="s" xmlns="http://www.w3.org/2000/svg" version="1.1" width="200px" height="200px">
          <rect width="200px" height="200px" style="fill:rgb(0,0,255);"/>
        </svg>
    </div>
</body>

var div = document.getElementById('d');
var rect = div.getBoundingClientRect();

alert(rect.width);  //200
alert(rect.height);  //205 (in safari), 204.5 in other browsers

var svg = document.getElementById('s');
var rect = svg.getBBox();

alert(rect.width);  //200
alert(rect.height);  //200

I'm trying to get the width and height of the parent div. 我想要获得父div的宽度和高度。 For whatever reason, getBoudingClientRect is giving me an incorrect height value (205 or 204.5) The width is correct, but the height is off. 无论出于何种原因,getBoudingClientRect给我一个不正确的高度值(205或204.5)宽度是正确的,但高度是关闭的。 Any ideas? 有任何想法吗?

http://jsfiddle.net/jTvaF/5/ http://jsfiddle.net/jTvaF/5/

Give the svg the property of display:block; 给svg提供display:block; and it should start outputting correctly. 它应该开始正确输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM