SVGSVGElement: preserveAspectRatio 属性

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

SVGSVGElement 接口的只读属性 preserveAspectRatio 反映了给定元素的 preserveAspectRatio 属性。它定义了 SVG 元素的 **内容** 如何缩放以适应给定空间,同时保持其纵横比。

一个 SVGAnimatedPreserveAspectRatio 对象。

示例

给定以下 SVG,我们可以使用 preserveAspectRatio 属性来检索 SVG 元素的缩放行为。

html
<svg
  xmlns="http://www.w3.org/2000/svg"
  viewBox="0 0 200 200"
  preserveAspectRatio="xMidYMid meet">
  <circle cx="100" cy="100" r="50" fill="blue" />
</svg>

我们可以访问 preserveAspectRatio 属性。

js
const svgElement = document.querySelector("svg");

console.log(svgElement.preserveAspectRatio.baseVal); // output: SVGPreserveAspectRatio {align: 1, meetOrSlice: 1}

规范

规范
Scalable Vector Graphics (SVG) 2
# __svg__SVGFitToViewBox__preserveAspectRatio

浏览器兼容性

另见