Restart background SVG animation

后端 未结 7 2128
广开言路
广开言路 2021-01-01 20:20

I have an SVG set as a background image of an element. The first time the element is displayed, the animation plays correctly.

On subsequent displays (e.g. if a dupl

7条回答
  •  情话喂你
    2021-01-01 20:36

    I think in many scenarios setCurrentTime(0) is the best solution -- if supported by the browser. Here is an example (assuming you use the tag for embedding, of course):

    let content = document.getElementById("object-id").contentDocument;
    let svg = content.getElementsByTagName("svg")[0];
    if(svg.getCurrentTime() > 10)
      svg.setCurrentTime(0);
    

    More infos about the relevant API can be found here: https://developer.mozilla.org/en-US/docs/Web/API/SVGSVGElement

    提交回复
    热议问题