SVGViewElement: preserveAspectRatio 属性

Baseline 已广泛支持

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

SVGViewElement 接口的只读属性 preserveAspectRatio 反映了给定 <view> 元素的 preserveAspectRatio 属性。它定义了 view 中的内容在保留其宽高比的情况下,应如何缩放到适应其视口。

一个 SVGAnimatedPreserveAspectRatio 对象。

示例

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

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

我们可以访问 preserveAspectRatio 属性。

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

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

规范

规范
Scalable Vector Graphics (SVG) 2
# __svg__SVGFitToViewBox__preserveAspectRatio

浏览器兼容性

另见