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

瀏覽器相容性

另見