2013-09-27 13 views
6
<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 

Próbuję uzyskać szerokość i wysokość div elementu nadrzędnego. Z jakiegokolwiek powodu getBoudingClientRect podaje mi nieprawidłową wartość wysokości (205 lub 204,5). Szerokość jest prawidłowa, ale wysokość jest wyłączona. Jakieś pomysły?getBoundingClientRect, niepoprawna wartość wysokości dla wbudowanego SVG

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

+0

Wysokość jest poprawna, ma 204,5 piksela wysokości. – Stijn

+0

powinno być 200px – user2800679

+3

możliwy duplikat [Istnieje 4px luka poniżej elementów canvas/wideo/audio w HTML5] (http://stackoverflow.com/questions/8600393/there-is-a-4px-gap-below- canvas-video-audio-elements-in-html5) – Stijn

Odpowiedz

3

Daj SVG właściwość display: block; i powinien rozpocząć poprawnie wyprowadzanie.

Powiązane problemy