【发布时间】:2022-01-24 05:40:04
【问题描述】:
我想为 svg 路径设置动画,使动画只运行一次,但是如果 d 值发生变化,那么动画应该再次运行一次。每次路径值更改时我都无法让动画运行,如果我将 repeatCount 值设置为 1,它在 svg 值更改和 svg“重新渲染”后将不会再次运行。我在下面包含了一些代码,这些代码复制了我正在尝试做的事情,感谢任何帮助。这是一个只有动画的代码演示,没有反应:https://codepen.io/shak8/pen/RwLLjNm
const SVGAnimate = () =>{
const [width, setWidth] = useState(213)
const [prevWidth, setPrevWidth] = useState(213)
return (
<div>
<svg width="400" height="500">
<path style="stroke-width:3;stroke:rgb(0,0,0)"
fill="black"
d=`M 0 212 S 226 212, ${width} 20 V 212 H 0`>
<animate
attributeName="d"
dur="5s"
from=`M 0 212 S 226 212,${prevWidth} 20 V 212 H 0`
to=`M 0 212 S 226 212, ${width} 20 V 212 H 0`
repeatCount="1"
fill="black"
/>
</path>
<button onClick={() => {
// These should trigger svg path to change, desired
// effect is for path to change with animation.
setPrevWidth(width == width)
setWidth(width == 213 ? 320 : 213)
}
}> Change</button>
</div>
)
}
我也尝试在路径上使用过渡,它在 chrome 上完美运行,但在 safari 上不起作用。
【问题讨论】:
标签: javascript css reactjs svg svg-animate