SVGLinearGradientElement: y2 属性

Baseline 已广泛支持

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

SVGLinearGradientElement 接口的只读属性 y2,以 SVGAnimatedLength 的形式描述了渐变起点的 y 轴坐标。它反映了 <linearGradient> 元素上 y2 属性的计算值。

属性值可以是 <length><percentage><number>SVGAnimatedLength.baseVal 的数值是渐变终点在用户坐标系中的 y 坐标。

一个 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>

我们可以访问 y2 属性的计算值

js
const linearGradients = document.querySelectorAll("linearGradient");
const y2Gradient1 = linearGradients[0].y2;
const y2Gradient2 = linearGradients[1].y2;

console.dir(y2Gradient1.baseVal.value); // output: 200 (100% of 200)
console.dir(y2Gradient2.baseVal.value); // output: 200 (100% of 200)

规范

规范
Scalable Vector Graphics (SVG) 2
# __svg__SVGLinearGradientElement__y2

浏览器兼容性

另见