jQuery animate() 函数
我有一个生命条(0% 生命值 = 0px 宽度;100% 生命值 = 100px):
<div style="width:<?php echo $health ?>"><?php echo $health ?></div>
用户单击“治愈”按钮后,他会收到一些生命值。使用 animate() 函数我使条形增加了宽度。
但我还需要其他东西:更新 div 内的健康值。我也想对其进行动画处理(具有与宽度相同的递增值)。
但我不知道怎么办。你能帮我吗?
LE:
$('div').animate({ width: data.newhealth }, duration: 1500);
它在 getJSON 函数内。
我认为解决方案是在动画过程中经常用 $('div').attr('width')
替换 div 的内容,但我也不知道该怎么做。
I have a health bar (0% health = 0px width; 100% health = 100px):
<div style="width:<?php echo $health ?>"><?php echo $health ?></div>
After the user clicks an "Heal" button, he receives some health. Using the animate()
function I made the bar to increase its width.
But I also need something else: to update the health value that is inside the div. I would like to animate this too (to have the same increasing values as the width).
But I don't know how. Can you help me please?
L.E.:
$('div').animate({ width: data.newhealth }, duration: 1500);
It's inside a getJSON function.
I thought the solution would be to replace the div's content with $('div').attr('width')
very often during the animation, but I don't know how to do this either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用动画函数的
step
选项。我这里有一个工作示例:http://jsfiddle.net/WxecS/重要的是:
You can use the
step
option to the animate function. I have a working example here: http://jsfiddle.net/WxecS/The important bit is:
这不会为文本设置动画,但会在最后更改它:
http://jsfiddle.net/Ncv7Z/4/
This wont animate the text, but will change it at the end:
http://jsfiddle.net/Ncv7Z/4/
使用
.animate()
的完整回调http://api.jquery.com/animate/
use complete callback of
.animate()
http://api.jquery.com/animate/
使用 step: 函数,如 jquery 站点 中所述
use the step: function like described on jquery site