【问题标题】:getBoundingClientRect, incorrect height value for inline SVGgetBoundingClientRect,内联 SVG 的高度值不正确
【发布时间】:2013-09-27 22:09:42
【问题描述】:
<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

我正在尝试获取父 div 的宽度和高度。无论出于何种原因,getBoudingClientRect 给了我一个不正确的高度值(205 或 204.5)宽度是正确的,但高度是关闭的。有什么想法吗?

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

【问题讨论】:

标签: javascript html svg getboundingclientrect


【解决方案1】:

给 svg 属性 display:block;它应该开始正确输出。

【讨论】:

    猜你喜欢
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2015-05-16
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 2015-12-06
    相关资源
    最近更新 更多