gpt4 book ai didi

javascript - 通过 Javascript 停止/启动 SVG 旋转

转载 作者:行者123 更新时间:2023-11-30 05:41:32 26 4
gpt4 key购买 nike

我需要一点帮助。在下面的代码中,我想通过 onmouse-event 来控制星星的旋转。

如果将鼠标移到星星上,它应该会旋转。

我考虑过通过 roationon()/off()attributeName 中的 transform 更改为不同的东西> 功能使旋转不起作用,但我不知道该怎么做。

我很感激能得到的每一个帮助。

谢谢

<!DOCTYPE html>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<html>

<body>

<script>
function rotationon() {}
function rotationoff() {}
</script>

<svg height="2000" width="2000">
<polygon id="stern1" points="100,10 40,180 190,60 10,60 160,180" fill="yellow" transform="translate(100, 100)" onmouseover="rotationon()" onmouseout="rotationoff()" >
<animateTransform
attributeName="transform"
begin="0s"
dur="5s"
type="rotate"
from="0 100 100"
to="360 100 100"
repeatCount="indefinite"
/>
</polygon>
</svg>


</body>
</html>

最佳答案

有几种不同的方法来解决这个问题,具体取决于它要集成的内容以及它与其他浏览器的配合情况。

我的直觉最终会说,值得使用其中一个 SVG 库,例如 Raphael、snap.svg、Pablo.js 等。它们将帮助解决可能面临的一些问题。

您也可以像我提到的那样使用纯 SVG http://jsfiddle.net/xaM6q/

但是,要使用您正在尝试的方法,您可能需要使用类似 beginElement() 和 endElement 的方法,因此代码可能类似于以下内容...fiddle at http://jsfiddle.net/xaM6q/2/

<script>
function rotationon(evt){
document.getElementById('myanim').beginElement();
}
function rotationoff(){
document.getElementById('myanim').endElement();
}
</script>

<svg height="2000" width="2000">
<g transform="translate(100,100)">
<polygon id="stern1" points="100,10 40,180 190,60 10,60 160,180" fill="yellow" onmouseover="rotationon()" onmouseout="rotationoff()" >
<animateTransform
id="myanim"
attributeName="transform"
begin="indefinite"
dur="5s"
type="rotate"
from="0 100 100"
to="360 100 100"
fill="freeze"
repeatCount="indefinite"
/>
</polygon>
</g>

有几件事值得注意。我已经添加了一个 g 元素来帮助保持转换到位,正如您可能想要的那样(没有它,您可能会发现它会移开)。此外,动画可能会有点不稳定,具体取决于您希望它如何停止(我添加了“fill=freeze”),以及动画中的事件会发生什么。

了解所有这些以了解 SVG 动画是值得的,但如前所述,我可能仍会考虑使用第 3 方库并手动控制旋转,而不是使用 animate 标签,因此您可以暂停/重新启动轻松旋转任意 Angular 。

关于javascript - 通过 Javascript 停止/启动 SVG 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20570651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com