为什么我的 Raphael JS 动画不循环播放?
你好呀 我使用 Raphael 框架制作了这个动画。我希望星星(logoStar)无限旋转,但它只运行一次。有人可以帮忙吗?谢谢
window.onload = function () {
buildLogo();
}
var buildLogo = function () {
var logo = Raphael("title",800,236);
var logoStar = logo.path("M12.245 131.057L16.039 138.743L24.521 139.974L18.383 145.958L19.832 154.406L12.245 150.418L4.658 154.406L6.108 145.958L-0.03 139.974L8.452 138.743").attr({fill:"#fff",stroke:"none"});
var starSpin = function () {
logoStar.animate({rotation: "360"}, 5000, starSpin);
}
starSpin();
}
Hi there
I've made this animation using the Raphael framework. I want the star (logoStar) to spin indefinitely but it only runs once. Can anyone help? Thanks
window.onload = function () {
buildLogo();
}
var buildLogo = function () {
var logo = Raphael("title",800,236);
var logoStar = logo.path("M12.245 131.057L16.039 138.743L24.521 139.974L18.383 145.958L19.832 154.406L12.245 150.418L4.658 154.406L6.108 145.958L-0.03 139.974L8.452 138.743").attr({fill:"#fff",stroke:"none"});
var starSpin = function () {
logoStar.animate({rotation: "360"}, 5000, starSpin);
}
starSpin();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从360°到360°的动画看起来没有动画,所以之前需要将旋转重置为零。
Animation from 360° to 360° looks like there no animation, so you need to reset rotation to zero before.
自从版本 2 推出以来,拥有无限循环动画的真正方法是这样的:
Infinity
是一个 Javascript 中的属性,因此不要将其作为字符串输入。这是一个有效的小提琴。
Since version 2 came out, the real way to have an infinitely looping animation is this:
Infinity
is a property in Javascript, so don't enter it as a string.Here's a working fiddle.
只是一个快速观察:
从版本 2 开始,“旋转”似乎不再是 Atrr 的一部分,因此您不能在“动画”中使用它,但您可以将其替换为“transform:”r“+(某种程度上)"..
例如:
Just a quick observation:
Looks like "Rotation" isn't part of the Atrr anymore since ver 2, so you can't use it in "animate", but you can replace that with "transform: "r" + (some degree)"..
eg:
我同意 Michael Mao 的观点,你应该加入
一个循环。
但像 while(true) 这样的循环并不总是一个好主意。它会占用太多 CPU,并且某些浏览器可能会警告用户脚本导致页面运行缓慢。也许最好在重新执行动画之前添加超时。尝试看看什么最有效;)
I agree with Michael Mao, you should put
in a loop.
But loops like this while(true) are not always a good idea. It will take too much CPU and some browsers may warn the user that a script is causing the page to run slowly. Maybe it is best to add a timeout before re-executing animate. Just try and see what works best ;)
我不完全确定,但我认为当您尝试在匿名函数中使用
starSpin
时,它尚未定义。尝试将:
更改为
编辑
事实证明,根本不是这样的——我不知道问题是什么(希望对拉斐尔图书馆有更多了解的人可以提供帮助),但是一个kludgey-but-work的解决方案是在调用回调中的整个构造函数之前清除有问题的元素(我警告过你这是杂乱的)。
参见: http://jsfiddle.net/rbf5x/1/
I'm not entirely sure, but I think that
starSpin
is not yet defined when you try to use it in the anonymous function.Try changing:
to
EDIT
It turns out that that's not it at all -- I do not know what the problem is (hopefully someone who has more exposure to the Raphael library can help out there) but a kludgey-but-works solution is to clear the element in question before calling the entire construct function in the callback (I warned you it was kludgey).
SEE: http://jsfiddle.net/rbf5x/1/
我不太确定,但是如果你这样做怎么办:
听起来该方法只执行一次,所以它“旋转”一次......
I am not very sure but what if you do this:
It sounds like the method is only executed once, so it "spins" once...