相关疑难解决方法(0)

获取SVG元素的宽度/高度

获取svg元素尺寸的正确方法是什么?

http://jsfiddle.net/langdonx/Xkv3X/

Chrome 28:

style x
client 300x100
offset 300x100
Run Code Online (Sandbox Code Playgroud)

IE 10:

stylex 
client300x100 
offsetundefinedxundefined 
Run Code Online (Sandbox Code Playgroud)

FireFox 23:

"style" "x"
"client" "0x0"
"offset" "undefinedxundefined"
Run Code Online (Sandbox Code Playgroud)

有宽度和高度属性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>
Run Code Online (Sandbox Code Playgroud)

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', …
Run Code Online (Sandbox Code Playgroud)

javascript svg

73
推荐指数
4
解决办法
8万
查看次数

类型 SVGElement 上不存在属性 getBBox

我正在使用 d3,我想使用 SVG 元素具有的 getBBox 方法。

我正在使用以下内容:(d3Selection.node() as SVGElement).getBBox(),但是由于标题中的错误,TypeScript 无法编译。

SVGElement 是错误的类型吗?我可以any改用它,但这似乎有点“不干净”的解决方案。

javascript svg types d3.js typescript

6
推荐指数
2
解决办法
3673
查看次数

标签 统计

javascript ×2

svg ×2

d3.js ×1

types ×1

typescript ×1