jQuery animated number counter from zero to value

后端 未结 10 638
天涯浪人
天涯浪人 2020-11-28 04:00

I have created a script to animate a number from zero to it\'s value.

Working

jQuery

相关标签:
10条回答
  • 2020-11-28 04:21

    IMPORTANT: It seems like a small difference but you should really use a data attribute to hold the original number to count up to. Altering the original number can have un-intended consequences. For instance, I'm having this animation run everytime an element enters the screen. But if the element enters, exits, and then enters the screen a second time before the first animation finishes, it will count up to the wrong number.

    HTML:

    <p class="count" data-value="200" >200</p>
    <p class="count" data-value="70" >70</p>
    <p class="count" data-value="32" >32</p>
    

    JQuery:

    $('.count').each(function () {
        $(this).prop('Counter', 0).animate({
                Counter: $(this).data('value')
            }, {
            duration: 1000,
            easing: 'swing',
            step: function (now) {                      
                $(this).text(this.Counter.toFixed(2));
            }
        });
    });
    
    0 讨论(0)
  • 2020-11-28 04:22

    What the code does, is that the number 8000 is counting up from 0 to 8000. The problem is, that it is placed at the middle of quite long page, and once user scroll down and actually see the number, the animation is already dine. I would like to trigger the counter, once it appears in the viewport.

    JS:

    $('.count').each(function () {
                    $(this).prop('Counter',0).animate({
                            Counter: $(this).text()
                    }, {
                            duration: 4000,
                            easing: 'swing',
                            step: function (now) {
                                    $(this).text(Math.ceil(now));
                            }
                    });
                });
    

    And HTML:

     <span class="count">8000</span>
    
    0 讨论(0)
  • 2020-11-28 04:24

    This is work for me !

    <script type="text/javascript">
    $(document).ready(function(){
            countnumber(0,40,"stat1",50);
            function countnumber(start,end,idtarget,duration){
                cc=setInterval(function(){
                    if(start==end)
                    {
                        $("#"+idtarget).html(start);
                        clearInterval(cc);
                    }
                    else
                    {
                       $("#"+idtarget).html(start);
                       start++;
                    }
                },duration);
            }
        });
    </script>
    <span id="span1"></span>
    
    0 讨论(0)
  • 2020-11-28 04:26

    You can do it with animate function in jQuery.

    $({ countNum: $('.code').html() }).animate({ countNum: 4000 }, {
        duration: 8000,
        easing: 'linear',
        step: function () {
            $('.yourelement').html(Math.floor(this.countNum));
        },
        complete: function () {
            $('.code').html(this.countNum);
            //alert('finished');
       }
    });
    

    Demo

    0 讨论(0)
  • 2020-11-28 04:31

    This worked for me

    HTML CODE

    <span class="number-count">841</span>
    

    jQuery Code

    $('.number-count').each(function () {
        $(this).prop('Counter',0).animate({
            Counter: $(this).text()
        }, {
            duration: 4000,
            easing: 'swing',
            step: function (now) {
                $(this).text(Math.ceil(now));
            }
        });
    
    0 讨论(0)
  • 2020-11-28 04:31

    This is working for me

    $('.Count').each(function () {
        $(this).prop('Counter',0).animate({
            Counter: $(this).text()
        }, {
            duration: 4000,
            easing: 'swing',
            step: function (now) {
                $(this).text(Math.ceil(now));
            }
        });
    });
    
    0 讨论(0)
提交回复
热议问题