how to animate numbers using Jquery

后端 未结 8 768
时光取名叫无心
时光取名叫无心 2021-02-02 02:31

I am trying to animate a say $233 to $250 or decreasing from 250 to 233 ,i dont want to replace 233 by 250 instead i want a counter kind of effect and at the time of scrolling

8条回答
  •  野性不改
    2021-02-02 03:09

    HTML

    
    
    
    

    JavaScript

    $(function ()
    {
        var $start = $('#start'),
            start = $start.get(0),
            $reset = $('#reset'),
            reset = $reset.get(0),
            $counter = $('#counter'),
            startVal = $counter.text(),
            currentVal = startVal,
            endVal = 250,
            prefix = '$',
            fontSize = $counter.css('font-size');
    
        $start.click(function ()
        {
            this.disabled = true;
            var i = setInterval(function ()
            {
                if (currentVal === endVal)
                {
                    clearInterval(i);
                    reset.disabled = false;
                    $counter.animate({fontSize: fontSize});
                }
                else
                {
                    currentVal++;
                    $counter.text(prefix+currentVal).animate({fontSize: '+=1'}, 100);
                }
            }, 100);
        });
    
        $reset.click(function ()
        {
            $counter.text(prefix + startVal);
            this.disabled = true;
            start.disabled = false;
        }).click();
    });
    

    Demo →

提交回复
热议问题