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

浏览器兼容性

另见