SVGUseElement: x 属性

Baseline 已广泛支持

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

SVGUseElement 接口的只读 **x** 属性,以 SVGAnimatedLength 的形式描述了引用元素的起始点的 x 轴坐标。它反映了 <use> 元素上 x 属性的计算值。

该属性的值可以是 <length><percentage><number>SVGAnimatedLength.baseVal 的数值是用户坐标系中引用元素的左上角的 x 坐标。

一个 SVGAnimatedLength 对象。

示例

给定以下 SVG

html
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <circle id="circle1" cx="50" cy="50" r="40" fill="blue" />
    <circle id="circle2" cx="50" cy="50" r="40" fill="red" />
  </defs>
  <use x="50%" y="50%" href="#circle1" />
  <use x="25%" y="50%" href="#circle2" />
</svg>

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

js
const uses = document.querySelectorAll("use");
const xUse1 = uses[0].x;
const xUse2 = uses[1].x;

console.log(xUse1.baseVal.value); // output: 100 (50% of 200)
console.log(xUse2.baseVal.value); // output: 50 (25% of 200)

规范

规范
Scalable Vector Graphics (SVG) 2
# __svg__SVGUseElement__x

浏览器兼容性

另见