SVGLinearGradientElement: x1 屬性

Baseline 已廣泛支援

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

SVGLinearGradientElement 介面的只讀屬性 x1 使用 SVGAnimatedLength 物件描述了漸變的起點的 x 軸座標。它反映了 <linearGradient> 元素上 x1 屬性的計算值。

屬性值可以是 <length><percentage><number>SVGAnimatedLength.baseVal 的數值是漸變起點在使用者座標系中的 x 座標。

一個 SVGAnimatedLength 物件。

示例

給定以下 SVG

html
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <linearGradient id="gradient1" x1="50%" y1="0%" x2="50%" y2="100%">
      <stop offset="0%" stop-color="blue" />
      <stop offset="100%" stop-color="white" />
    </linearGradient>
    <linearGradient id="gradient2" x1="25%" y1="0%" x2="75%" y2="100%">
      <stop offset="0%" stop-color="red" />
      <stop offset="100%" stop-color="yellow" />
    </linearGradient>
  </defs>
  <rect x="0" y="0" width="200" height="100" fill="url(#gradient1)" />
  <rect x="0" y="100" width="200" height="100" fill="url(#gradient2)" />
</svg>

我們可以訪問 x1 屬性的計算值

js
const linearGradients = document.querySelectorAll("linearGradient");
const x1Gradient1 = linearGradients[0].x1;
const x1Gradient2 = linearGradients[1].x1;

console.dir(x1Gradient1.baseVal.value); // output: 100 (50% of 200)
console.dir(x1Gradient2.baseVal.value); // output: 50 (25% of 200)

規範

規範
Scalable Vector Graphics (SVG) 2
# __svg__SVGLinearGradientElement__x1

瀏覽器相容性

另見