SVGUseElement: width 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

SVGUseElement 介面的只讀屬性 width 使用 SVGAnimatedLength 物件描述引用的元素的寬度。它反映了 <use> 元素上 width 屬性的計算值。

屬性值可以是 <length><percentage><number>SVGAnimatedLength.baseVal 的數值是使用者座標系中引用元素的寬度。

一個 SVGAnimatedLength 物件。

示例

給定以下 SVG

html
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <circle id="circle1" cx="50" cy="50" r="40" fill="blue" />
    <circle id="circle2" cx="50" cy="50" r="40" fill="red" />
  </defs>
  <use x="50%" y="50%" href="#circle1" width="50" />
  <use x="25%" y="50%" href="#circle2" width="100" />
</svg>

我們可以訪問 width 屬性的計算值

js
const uses = document.querySelectorAll("use");
const widthUse1 = uses[0].width;
const widthUse2 = uses[1].width;

console.log(widthUse1.baseVal.value); // output: 50
console.log(widthUse2.baseVal.value); // output: 100

規範

規範
Scalable Vector Graphics (SVG) 2
# __svg__SVGUseElement__width

瀏覽器相容性

另見