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

SVGTextPathElement: side property

· 52CE 我爱测

The **`side`** read-only property of the SVGTextPathElement interface represents the side of the path that the text is placed on (relative to the path direction).

SVG

The side read-only property of the SVGTextPathElement interface represents the side of the path that the text is placed on (relative to the path direction).

The side.baseVal property reflects the side content attribute of the given textPath element, as an enumerated value. While side is read-only, you can use side.baseVal to modify the value of the corresponding attribute.

In SVG 2, side.animVal also reflects the non-animated value of the attribute.

Value

An SVGAnimatedEnumeration object.

The following static properties indicate the values that can be returned from side.baseVal (and side.animVal):

Note that baseVal cannot be set to 0 (TEXTPATH_SIDETYPE_UNKNOWN) or any value other than those listed above. animVal is read-only and with throw if you attempt to write to it.

Examples

Also see the Example in SVGTextPathElement, which allows you to toggle the side attribute.

Accessing the side property

This example demonstrates how you can set and get the side property, and in particular its baseVal.

HTML

First we define HTML and CSS for an SVG path that draws text on the right on supporting browsers using the SVG side attribute.

html,
body,
svg {
  height: 400px;
  width: auto; /* Keeps the aspect ratio */
}
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  
  
  <path
    id="MyPath"
    fill="none"
    stroke="red"
    d="M10,90 Q90,90 90,45 Q90,10 50,10 Q10,10 10,40 Q10,70 45,70 " />

  <text>
    <textPath href="#MyPath" side="right">This text follows a path.</textPath>
  </text>
</svg>

We also add a button for toggling the value of the side.baseVal property. Note that there is also logging code, that is hidden as it is not relevant.

<button id="toggle-side">Toggle Side</button>
<pre id="log"></pre>
#log {
  height: 50px;
  overflow: scroll;
  padding: 0.5rem;
  border: 1px solid black;
}
const logElement = document.querySelector("#log");
function log(text) {
  logElement.innerText = `${logElement.innerText}${text}\n`;
  logElement.scrollTop = logElement.scrollHeight;
}

JavaScript

The code below first gets the side.baseVal property and compares it to the enumerated static property values to determine (and log) which side of the path the text is drawn on. If the side property isn't defined the code will throw, and we note that the side property isn't supported.

const textPath = document.querySelector("textPath");
const button = document.querySelector("#toggle-side");

// Helper function to read and log the current side
function logCurrentSide() {
  try {
    let side;

    if (textPath.side.baseVal === SVGTextPathElement.TEXTPATH_SIDETYPE_RIGHT) {
      side = "right";
    } else if (
      textPath.side.baseVal === SVGTextPathElement.TEXTPATH_SIDETYPE_LEFT
    ) {
      side = "left";
    } else if (
      textPath.side.baseVal === SVGTextPathElement.TEXTPATH_SIDETYPE_UNKNOWN
    ) {
      side = "unknown";
    } else {
      side = "unexpected value";
    }
    log(`Current side: ${side}`);
  } catch {
    log(`side property is not supported in this browser`);
  }
}

// Log the initial state on load
logCurrentSide();

The code below shows how you can set side.baseVal with the enumerated static values. The event handler first checks the current value of side.baseVal and then toggles the value to the static property that matches the other side.

// Toggle the side when the button is clicked
button.addEventListener("click", () => {
  try {
    if (textPath.side.baseVal === SVGTextPathElement.TEXTPATH_SIDETYPE_RIGHT) {
      // Change to left
      textPath.side.baseVal = SVGTextPathElement.TEXTPATH_SIDETYPE_LEFT;
    } else {
      // Change to right
      textPath.side.baseVal = SVGTextPathElement.TEXTPATH_SIDETYPE_RIGHT;
    }

    // Log the updated state
    logCurrentSide();
  } catch (e) {
    log("Setting the side property is not supported in this browser.");
  }
});

Result

Toggle the button to move the text from one side to the other.

Accessing the side property

Specifications

Browser compatibility

See also

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

资料来源与适用范围

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

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

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