SVGSymbolElement: preserveAspectRatio 屬性

Baseline 已廣泛支援

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

SVGSymbolElement 介面的只讀屬性 preserveAspectRatio 反映了給定 <symbol> 元素的 preserveAspectRatio 屬性。它定義了 symbol 的內容應如何縮放到適應給定空間,同時保留其寬高比。

一個 SVGAnimatedPreserveAspectRatio 物件。

示例

鑑於以下 SVG,我們可以使用 preserveAspectRatio 屬性來檢索 symbol 元素的縮放行為。

html
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">
  <defs>
    <symbol
      id="exampleSymbol"
      viewBox="0 0 100 100"
      preserveAspectRatio="xMinYMin meet">
      <circle cx="50" cy="50" r="50" fill="blue" />
    </symbol>
  </defs>
  <use href="#exampleSymbol" x="50" y="50" width="100" height="100" />
</svg>

我們可以訪問 preserveAspectRatio 屬性。

js
const symbolElement = document.getElementById("exampleSymbol");

// Access the preserveAspectRatio property
const aspectRatio = symbolElement.preserveAspectRatio.baseVal;

console.log(aspectRatio.align); // Output: 2 (xMinYMin)
console.log(aspectRatio.meetOrSlice); // Output: 1 (meet)

規範

規範
Scalable Vector Graphics (SVG) 2
# __svg__SVGFitToViewBox__preserveAspectRatio

瀏覽器相容性

另見