使用 JQuery 制作动画数字(半倒计时)?

发布于 2024-08-09 10:12:29 字数 700 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慕巷 2024-08-16 10:12:29

您在这里所做的就是快速连续多次更新 DOM。因此,浏览器将等到您完成所有更改后,才会重新绘制页面。因此,直到数字一直下降到 4000 为止,您不会看到任何视觉变化。

是的,您确实需要使用 setTimeoutsetInterval / <代码>clearInterval。或者,为了代码清晰,您可以使用 jQuery“wait”插件:

// (code to get new value goes here)

$('.class').wait(100, function(){
    $(this).text(newValue);
});

我使用了 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 or setInterval / clearInterval. Or, for clarity of code, you could use the jQuery "wait" plugin:

// (code to get new value goes here)

$('.class').wait(100, function(){
    $(this).text(newValue);
});

Instead of html(), I've used text(), since it looks like you don't need to change any HTML structure.

挽梦忆笙歌 2024-08-16 10:12:29

当我运行您提供的代码时,我陷入了无限循环。在 do 循环的末尾,有

current_value = parseInt(numb);

但 numb 的值仅在函数开始时设置,因此它会永远持续下去。如果将其更改为

current_value = parseInt($(id).html());

则它可以正常工作。但它似乎是立即发生的。

我破解了一种使用超时来实现动画的方法,该方法似乎工作得相当好,但由于我对 javascript 还很陌生,我不知道是否有更有效的方法。只需调整传递给 setTimeout 的第二个参数即可获得所需的速度。而如果你想改变增量/减量值,只需改变dir的减速度即可。

function mod2(id, value) {
    var numb = $(id).html();
    var current_value = parseInt(numb);

    // determine direction to go
    var dir = 1;
    if (current_value - value > 0) {
        dir *= -1;
    }
    getThere(id, current_value, value, dir);
}

function getThere(id, current_value, target_value, dir) {
    current_value += dir;
    $(id).html(current_value);
    if (current_value != target_value) {
        setTimeout("getThere('"+id+"',"+current_value+","+target_value+","+dir+")", 10);
    }
}

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.

function mod2(id, value) {
    var numb = $(id).html();
    var current_value = parseInt(numb);

    // determine direction to go
    var dir = 1;
    if (current_value - value > 0) {
        dir *= -1;
    }
    getThere(id, current_value, value, dir);
}

function getThere(id, current_value, target_value, dir) {
    current_value += dir;
    $(id).html(current_value);
    if (current_value != target_value) {
        setTimeout("getThere('"+id+"',"+current_value+","+target_value+","+dir+")", 10);
    }
}
晚风撩人 2024-08-16 10:12:29

我喜欢 thorn 的 setTimeout 方法,但我会将其压缩为 2 个函数,并在窗口加载后启动它,以确保页面在更新计数器之前已加载:

var counterTimeout = 10; // time between increments in ms
$(window).load(function() {
    mod('class', 'add', 4000);
});

function mod(class, type, targetVal) {
    var $class = $(class);
    var numb = parseInt($class.html());
    numb = (type == 'add') ? numb + 1 : numb - 1;
    $class.html(numb);
    if (numb != targetVal) {
        setTimeout('mod("' + class + '","' + type + '",' + targetVal)', counterTimeout);
    }
}

在事件 $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:

var counterTimeout = 10; // time between increments in ms
$(window).load(function() {
    mod('class', 'add', 4000);
});

function mod(class, type, targetVal) {
    var $class = $(class);
    var numb = parseInt($class.html());
    numb = (type == 'add') ? numb + 1 : numb - 1;
    $class.html(numb);
    if (numb != targetVal) {
        setTimeout('mod("' + class + '","' + type + '",' + targetVal)', counterTimeout);
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文