SVGSymbolElement:viewBox 屬性

Baseline 已廣泛支援

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

SVGSymbolElement 介面的只讀屬性 viewBox 反映了給定 <symbol> 元素的 viewBox 屬性。

一個 SVGAnimatedRect 物件。

示例

訪問 viewBox 屬性

html
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">
  <defs>
    <symbol id="exampleSymbol" viewBox="0 0 100 100">
      <circle cx="50" cy="50" r="50" fill="blue" />
    </symbol>
  </defs>
  <use href="#exampleSymbol" x="50" y="50" width="100" height="100" />
</svg>
js
const symbolElement = document.getElementById("exampleSymbol");

// Access the viewBox property
const viewBox = symbolElement.viewBox.baseVal;

console.log(viewBox.x); // Output: 0
console.log(viewBox.y); // Output: 0
console.log(viewBox.width); // Output: 100
console.log(viewBox.height); // Output: 100

規範

規範
Scalable Vector Graphics (SVG) 2
# __svg__SVGFitToViewBox__viewBox

瀏覽器相容性

另見