How to start counter from 0 rather than maximum value?

Sachin

I have created a jquery number counter which displays maximum value (the value written in HTML) and increases the number by one to attain maximum value when you click on the body of page. But when the page loads, the number displayed always to maximum value. I want the number to be equal to zero when it loads first time. Here's my code:

HTML

<div class="mydiv"><span class="count">100</span></div>
<div class="mydiv"><span class="count">200</span></div>
<div class="mydiv"><span class="count">300</span></div>

CSS

.mydiv {
    width:100px;
    height:100px;
    background:red;
    -moz-border-radius:50px;
    -webkit-border-radius:50px;
    border-radius:50px;
    float:left;
    margin:5px;
}

.count {
    line-height:100px;
    color:white;
    margin-left:30px;
    font-size:25px;
}

JS

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

I am giving demo here.

rrk

Use a data- attribute to set max value other that the html. Then you can set the html as 0.

$(document).ready(function() {
  $(window).click(function() {
    $('.count').each(function() {
      $(this).prop('Counter', 0).animate({
        Counter: $(this).data('limit')
      }, {
        duration: 4000,
        easing: 'swing',
        step: function(now) {
          $(this).text(Math.ceil(now));
        }
      });
    });
  });
});
.mydiv {
  width: 100px;
  height: 100px;
  background: red;
  -moz-border-radius: 50px;
  -webkit-border-radius: 50px;
  border-radius: 50px;
  float: left;
  margin: 5px;
}

.count {
  line-height: 100px;
  color: white;
  margin-left: 30px;
  font-size: 25px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="mydiv"><span class="count" data-limit="100">0</span></div>
<div class="mydiv"><span class="count" data-limit="200">0</span></div>
<div class="mydiv"><span class="count" data-limit="300">0</span></div>

DEMO

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JS chart knob: animate from value to value rather than 0

From Dev

How to get a value from an <input> rather than a prompt?

From Dev

How to start CImg drawing from bottom-left corner rather than top-left corner?

From Dev

How do I set the replaced value in a sparse matrix to NA rather than 0?

From Dev

How to get actual move rather than move value from mini max algorithm

From Dev

How to get actual move rather than move value from mini max algorithm

From Dev

How to make PMD run at the start of the maven build rather than at the end of it?

From Dev

Excel VBA Array from Range Starting at -1 rather than 0

From Dev

Excel VBA Array from Range Starting at -1 rather than 0

From Dev

How to use 0 in variable as a string rather than boolean in || operation?

From Dev

Get value from IO rather than the computation itself

From Dev

Partial index on value from related table, rather than foreign key?

From Dev

How to get CSS height value (rather than calculated height) in jQuery

From Dev

Excel - How to get expression text rather than the value

From Dev

Perl - DBI - How to process array rather than single value?

From Dev

How to display variable name itself rather than the value in shell

From Dev

How to store data in Django cache as a reference rather than value.?

From Dev

Python: How to demand a string as input rather than a specific value

From Dev

How to get value greater than 0 from table in Sqlite

From Dev

Force bars to start from a lower value than 0 in ggplot geom_bar in R

From Dev

How to start a for loop from the end of a vector, and at the value 0 do something

From Dev

How to use field rather than accessor from a closure in Groovy

From Dev

How to append to a file from stream rather than overwrite in Python

From Dev

How to get the Coloumn Name from Model rather than Primary Key

From Dev

How to set MFMailComposeViewController From field rather than default mail app

From Dev

What API Is Needed To Run A Delphi App from the Start Screen Rather Than The Desktop

From Dev

Why start from XSD (rather than WSDL) in contract-first SOAP-service?

From Dev

Show week numbers on datepicker starting from current week rather than start of year

From Dev

Letter Counter (with substring rather than the usual atChar() method)

Related Related

  1. 1

    JS chart knob: animate from value to value rather than 0

  2. 2

    How to get a value from an <input> rather than a prompt?

  3. 3

    How to start CImg drawing from bottom-left corner rather than top-left corner?

  4. 4

    How do I set the replaced value in a sparse matrix to NA rather than 0?

  5. 5

    How to get actual move rather than move value from mini max algorithm

  6. 6

    How to get actual move rather than move value from mini max algorithm

  7. 7

    How to make PMD run at the start of the maven build rather than at the end of it?

  8. 8

    Excel VBA Array from Range Starting at -1 rather than 0

  9. 9

    Excel VBA Array from Range Starting at -1 rather than 0

  10. 10

    How to use 0 in variable as a string rather than boolean in || operation?

  11. 11

    Get value from IO rather than the computation itself

  12. 12

    Partial index on value from related table, rather than foreign key?

  13. 13

    How to get CSS height value (rather than calculated height) in jQuery

  14. 14

    Excel - How to get expression text rather than the value

  15. 15

    Perl - DBI - How to process array rather than single value?

  16. 16

    How to display variable name itself rather than the value in shell

  17. 17

    How to store data in Django cache as a reference rather than value.?

  18. 18

    Python: How to demand a string as input rather than a specific value

  19. 19

    How to get value greater than 0 from table in Sqlite

  20. 20

    Force bars to start from a lower value than 0 in ggplot geom_bar in R

  21. 21

    How to start a for loop from the end of a vector, and at the value 0 do something

  22. 22

    How to use field rather than accessor from a closure in Groovy

  23. 23

    How to append to a file from stream rather than overwrite in Python

  24. 24

    How to get the Coloumn Name from Model rather than Primary Key

  25. 25

    How to set MFMailComposeViewController From field rather than default mail app

  26. 26

    What API Is Needed To Run A Delphi App from the Start Screen Rather Than The Desktop

  27. 27

    Why start from XSD (rather than WSDL) in contract-first SOAP-service?

  28. 28

    Show week numbers on datepicker starting from current week rather than start of year

  29. 29

    Letter Counter (with substring rather than the usual atChar() method)

HotTag

Archive