此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

y2

基线
广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

y2 属性用于为需要多个坐标才能绘制的 SVG 元素指定第二个 y 坐标。只需一个坐标的元素则使用 y 属性。

元素

你可以将此属性与以下各节所述的 SVG 元素一起使用。

<line>

对于 <line>y2 定义线段终点的 y 坐标。

<length>|<percentage>|<number>
默认值 0
动画性
html
<svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">
  <line x1="1" x2="9" y1="5" y2="1" stroke="red" />
  <line x1="1" x2="9" y1="5" y2="5" stroke="green" />
  <line x1="1" x2="9" y1="5" y2="9" stroke="blue" />
</svg>

<linearGradient>

对于 <linearGradient>y2 定义用于映射色标值的渐变向量终点的 y 坐标。该属性的具体行为受 gradientUnits 属性影响。

<length>
默认值 0%
动画性
html
<svg viewBox="0 0 20 10" xmlns="http://www.w3.org/2000/svg">
  <!--
  默认情况下,渐变向量终止于其所应用到的形状的包围盒右上角。
  -->
  <linearGradient y2="0%" id="g0">
    <stop offset="5%" stop-color="black" />
    <stop offset="50%" stop-color="red" />
    <stop offset="95%" stop-color="black" />
  </linearGradient>

  <rect x="1" y="1" width="8" height="8" fill="url(#g0)" />

  <!--
  此处渐变向量终止于其所应用到的形状的包围盒右下角。
  -->
  <linearGradient y2="100%" id="g1">
    <stop offset="5%" stop-color="black" />
    <stop offset="50%" stop-color="red" />
    <stop offset="95%" stop-color="black" />
  </linearGradient>

  <rect x="11" y="1" width="8" height="8" fill="url(#g1)" />
</svg>

示例

html
<svg viewBox="0 0 25 25" xmlns="http://www.w3.org/2000/svg">
  <line x1="2" x2="22" y1="20" y2="0" stroke="red" />
  <line x1="2" x2="22" y1="20" y2="10" stroke="green" />
  <line x1="2" x2="22" y1="20" y2="20" stroke="blue" />
</svg>

规范

规范
Scalable Vector Graphics (SVG) 2
# LineElementY2Attribute
Scalable Vector Graphics (SVG) 2
# LinearGradientElementY2Attribute

浏览器兼容性

svg.elements.line.y2

svg.elements.linearGradient.y2