SVGSVGElement: setCurrentTime() 方法

Baseline 已广泛支持

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

SVGSVGElement 接口的 setCurrentTime() 方法用于调整此 SVG 文档片段的时钟,从而建立一个新的当前时间。

如果在文档时间线开始之前调用 setCurrentTime()(例如,在文档的 SVGLoad 事件触发之前,由 <script> 元素中运行的脚本调用),那么该方法最后一次调用的秒值将是文档在时间线开始后将跳转到的时间。

语法

js
setCurrentTime(time)

参数

时间

一个浮点数,表示要设置的当前时间的秒数。

返回值

无。

示例

设置当前时间

html
<svg
  id="exampleSVG"
  width="200"
  height="200"
  xmlns="http://www.w3.org/2000/svg">
  <circle id="circle1" cx="100" cy="100" r="50" fill="blue" />
</svg>
<button id="setTimeButton">Set Current Time</button>
<p id="currentTimeDisplay"></p>
js
const svgElement = document.getElementById("exampleSVG");
const setTimeButton = document.getElementById("setTimeButton");
const currentTimeDisplay = document.getElementById("currentTimeDisplay");

setTimeButton.addEventListener("click", () => {
  // Setting the time to 5 seconds
  svgElement.setCurrentTime(5);
  const currentTime = svgElement.getCurrentTime();
  currentTimeDisplay.textContent = `Current time in the SVG: ${currentTime} seconds`;
});

规范

规范
SVG 动画级别 2
# __svg__SVGSVGElement__setCurrentTime

浏览器兼容性