大网数据广告活动
Web 标准与前端开发

SVGLengthList

· 52CE 我爱测

The **`SVGLengthList`** interface defines a list of SVGLength objects. It is used for the SVGAnimatedLengthList.baseVal and SVGAnimatedLengthList.animVal properties of SVGAnimatedLengthList.

SVG

The SVGLengthList interface defines a list of SVGLength objects. It is used for the SVGAnimatedLengthList.baseVal and SVGAnimatedLengthList.animVal properties of SVGAnimatedLengthList.

An SVGLengthList object can be designated as read only, which means that attempts to modify the object will result in an exception being thrown.

An SVGLengthList object is indexable and can be accessed like an array using bracket notation. Reading an index is equivalent to calling SVGLengthList.getItem. Assigning to an index is equivalent to calling SVGLengthList.replaceItem, including the exceptions it throws.

Instance properties

  • SVGLengthList.length
    • : The number of items in the list.
  • SVGLengthList.numberOfItems
    • : The number of items in the list.

Instance methods

  • SVGLengthList.appendItem
    • : Inserts a new item at the end of the list.
  • SVGLengthList.clear
    • : Clears all existing items from the list, with the result being an empty list.
  • SVGLengthList.initialize
    • : Clears all existing items from the list and re-initializes the list to hold the single item specified by the parameter.
  • SVGLengthList.getItem
    • : Returns the specified item from the list.
  • SVGLengthList.insertItemBefore
    • : Inserts a new item into the list at the specified position.
  • SVGLengthList.removeItem
    • : Removes an existing item from the list.
  • SVGLengthList.replaceItem
    • : Replaces an existing item in the list with a new item.

Examples

Using SVGLengthList

An SVGLengthList object can be retrieved from an SVGAnimatedLengthList object, which itself is retrievable from many animatable length attributes, such as SVGTextPositioningElement.x.

HTML

<svg
  viewBox="0 0 200 100"
  xmlns="http://www.w3.org/2000/svg"
  width="200"
  height="100">
  <text id="text1" x="10" y="50">Hello</text>
</svg>
<button id="equally-distribute">Equally distribute letters</button>
<button id="reset-spacing">Reset spacing</button>
<div>
  <b>Current <code>SVGLengthList</code></b>
  <pre><output id="output"></output></pre>
</div>

JavaScript

const text = document.getElementById("text1");
const output = document.getElementById("output");
const list = text.x.baseVal;
function equallyDistribute() {
  list.clear();
  for (let i = 0; i < text.textContent.length; i++) {
    const length = text.ownerSVGElement.createSVGLength();
    length.value = i * 20 + 10;
    list.appendItem(length);
  }
  printList();
}
function resetSpacing() {
  const length = text.ownerSVGElement.createSVGLength();
  length.value = 10;
  list.initialize(length);
  printList();
}
function printList() {
  output.textContent = "";
  for (let i = 0; i < list.length; i++) {
    output.innerText += `${list.getItem(i).value}\n`;
  }
}
printList();

document
  .getElementById("equally-distribute")
  .addEventListener("click", equallyDistribute);
document
  .getElementById("reset-spacing")
  .addEventListener("click", resetSpacing);

Result

Using SVGLengthList

Specifications

Browser compatibility

See also

  • SVGNumberList
  • SVGPointList
  • SVGStringList
  • SVGTransformList

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。