获取SVG元素的尺寸的正确方法是什么?
IE 10:
火狐浏览器23:
小提琴长这个样子:
CSS(层叠样式表)
http://jsfiddle.net/langdonx/Xkv3X/
Chrome 28:style x
client 300x100
offset 300x100
IE 10:
stylex
client300x100
offsetundefinedxundefined
火狐浏览器23:
"style" "x"
"client" "0x0"
"offset" "undefinedxundefined"
在svg1
上有宽度和高度属性,但只有在我设置元素的宽度和高度属性时,.width.baseVal.value
才会被设置。
小提琴长这个样子:
HTML
<svg id="svg1" xmlns="http://www.w3.org/2000/svg" version="1.1">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="1" fill="red" />
<circle cx="150" cy="50" r="40" stroke="black" stroke-width="1" fill="green" />
<circle cx="250" cy="50" r="40" stroke="black" stroke-width="1" fill="blue" />
</svg>
JS
var svg1 = document.getElementById('svg1');
console.log(svg1);
console.log('style', svg1.style.width + 'x' + svg1.style.height);
console.log('client', svg1.clientWidth + 'x' + svg1.clientHeight);
console.log('offset', svg1.offsetWidth + 'x' + svg1.offsetHeight);
CSS(层叠样式表)
#svg1 {
width: 300px;
height: 100px;
}
clientWidth
和clientHeight
对我来说起到了作用。 - iPadDeveloper2011