使用 JQuery 制作动画数字(半倒计时)?
我正在尝试使用 JQuery 将一个数值(例如 5000)快速更改为另一个值(例如 4000)。现在我用以下方法做得很好:
mod(".class",4000,"add");
function mod(id,value,type){
var numb = $(id).html();
var current_value = parseInt(numb);
do {
if(type == "add")
increment(id);
else
decrement(id);
current_value = parseInt(numb);
}while(current_value != value);
function decrement(id){
$(id).html(current_value-1);
}
function increment(id){
$(id).html(current_value+1);
}
}
我知道这可能不是最好的方法,但我需要它做的是从当前值到设定值非常快速地倒计时(或向上)数字。我使用此方法的目的是使用 setInterval 或 setTimeout 进行延迟,但这会使整个脚本严重失败。
任何建议都是值得赞赏的,但是我不希望使用大型插件来完成这个看似简单的任务。
I am trying to make a numerical value, say 5000, quickly change to another value, say 4000, using JQuery. Right now I do this fine using:
mod(".class",4000,"add");
function mod(id,value,type){
var numb = $(id).html();
var current_value = parseInt(numb);
do {
if(type == "add")
increment(id);
else
decrement(id);
current_value = parseInt(numb);
}while(current_value != value);
function decrement(id){
$(id).html(current_value-1);
}
function increment(id){
$(id).html(current_value+1);
}
}
I know it's probably not the best way to go about it but what I need for it to do is countdown (or up) the numbers very quickly from the current value to the set value. What I intended with this method was to have a delay using setInterval or setTimeout however that makes the whole script fail pretty badly.
Any advice is appreciated, however I would prefer not to use large plugins for this seemingly simple task.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在这里所做的就是快速连续多次更新 DOM。因此,浏览器将等到您完成所有更改后,才会重新绘制页面。因此,直到数字一直下降到 4000 为止,您不会看到任何视觉变化。
是的,您确实需要使用
setTimeout
或setInterval
/ <代码>clearInterval。或者,为了代码清晰,您可以使用 jQuery“wait”插件:我使用了
text()
,而不是html()
,因为看起来您不需要更改任何 HTML 结构。What you are doing here is updating the DOM many times in quick succession. As a result, the browser will wait until you've done all your changes, and only then will it re-draw the page. So, you won't get to see any visual change until the number goes all the way down to 4000.
Yes, you do need to use a
setTimeout
orsetInterval
/clearInterval
. Or, for clarity of code, you could use the jQuery "wait" plugin:Instead of
html()
, I've usedtext()
, since it looks like you don't need to change any HTML structure.当我运行您提供的代码时,我陷入了无限循环。在 do 循环的末尾,有
current_value = parseInt(numb);
但 numb 的值仅在函数开始时设置,因此它会永远持续下去。如果将其更改为
current_value = parseInt($(id).html());
则它可以正常工作。但它似乎是立即发生的。
我破解了一种使用超时来实现动画的方法,该方法似乎工作得相当好,但由于我对 javascript 还很陌生,我不知道是否有更有效的方法。只需调整传递给 setTimeout 的第二个参数即可获得所需的速度。而如果你想改变增量/减量值,只需改变
dir
的减速度即可。When I ran the code you supplied, I was caught in an infinite loop. At the end of the do loop, you have
current_value = parseInt(numb);
but the value of numb is only set at the beginning of the function, so it goes on forever. If you change that to
current_value = parseInt($(id).html());
then it works fine. Except that it appears to happen instantly.
I hacked up a method to achieve the animation using timeouts that seems to work fairly well, but as I'm still fairly new to javascript I don't know if there is a more efficient approach or not. Just tweak the second param passed to setTimeout to get the desired speed. And if you want to change the increment/decrement value, just change the deceleration of
dir
.我喜欢 thorn 的 setTimeout 方法,但我会将其压缩为 2 个函数,并在窗口加载后启动它,以确保页面在更新计数器之前已加载:
在事件 $class.html() 中不满足基本情况在“add”的情况下以高于 targetVal 的值开始,在其他情况下以低于 targetVal 的值开始。您必须确保在调用函数之前不会发生这种情况。
I like thorn's approach with setTimeout, but I would condense it down to 2 functions and start it after window load to ensure the page has loaded prior to updating the counter:
The base case isn't satisfied in the event $class.html() starts with a value higher than the targetVal in the case of 'add' or lower than the targetVal in the other case. You would have to ensure this does not happen prior to making the function call.