SVGPatternElement: viewBox 屬性

Baseline 已廣泛支援

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

SVGPatternElement 介面的只讀屬性 viewBox 反映了給定元素的 viewBox 屬性。它表示定義了用於模式 viewBoxxywidthheight 值。

一個 SVGAnimatedRect 物件。

示例

給定以下 SVG,我們可以使用 viewBox 屬性來檢索模式元素的 viewBox 的尺寸。

html
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <pattern id="pattern1" width="10" height="10" viewBox="0 0 50 50">
      <circle cx="25" cy="25" r="25" fill="blue" />
    </pattern>
  </defs>
  <rect x="0" y="0" width="100" height="100" fill="url(#pattern1)" />
</svg>

我們可以訪問 viewBox 屬性。

js
const pattern = document.querySelector("pattern");

console.log(pattern.viewBox.baseVal); // output: DOMRect {x: 0, y: 0, width: 50, height: 50}

規範

規範
Scalable Vector Graphics (SVG) 2
# __svg__SVGFitToViewBox__viewBox

瀏覽器相容性

另見