countup.mdx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. ---
  2. title: Countup
  3. description: A countup element is used to display numerical data in an interesting way and make the interface more interactive.
  4. libs: countup
  5. ---
  6. To be able to use the countup in your application you will need to install the countup.js dependency with `npm install countup.js`.
  7. ## Default countup
  8. To create a countup, add `data-countup` to any HTML text tag and specify the number which is to be reached. The animation will be triggered as soon as the number enters the viewport.
  9. ```html code example
  10. <script src="$TABLER_CDN/dist/libs/countup.js/dist/countUp.js"></script>
  11. <h1 data-countup>30000</h1>
  12. ```
  13. ## Duration
  14. Set the `duration` to determine how long the animation should take. By default, the duration is set to 2 seconds, but you can modify it as you wish.
  15. ```html code example
  16. <script src="$TABLER_CDN/dist/libs/countup.js/dist/countUp.js"></script>
  17. <h1 data-countup>30000</h1>
  18. <h1 data-countup='{"duration":4}'>30000</h1>
  19. <h1 data-countup='{"duration":6}'>30000</h1>
  20. ```
  21. ## Starting value
  22. By default the countup will start from zero. If you want to set a different start value use `startVal`.
  23. You can also set the start value to be greater than the final value, so that it counts down instead of up.
  24. ```html code example
  25. <script src="$TABLER_CDN/dist/libs/countup.js/dist/countUp.js"></script>
  26. <h1 data-countup='{"startVal":12345}'>30000</h1>
  27. <h1 data-countup='{"startVal":47655}'>30000</h1>
  28. ```
  29. ## Decimal places
  30. Set how many decimal numbers should be displayed using `decimalPlaces`.
  31. ```html code example
  32. <script src="$TABLER_CDN/dist/libs/countup.js/dist/countUp.js"></script>
  33. <h1 data-countup>3.123</h1>
  34. <h1 data-countup='{"decimalPlaces":1}'>3.123</h1>
  35. <h1 data-countup='{"decimalPlaces":2}'>3.123</h1>
  36. <h1 data-countup='{"decimalPlaces":3}'>3.123</h1>
  37. ```
  38. ## Easing
  39. Disable easing using `"useEasing": false`. Easing is set to `true` by default, so that the animation speed changes over the course of its duration.
  40. ```html code example
  41. <script src="$TABLER_CDN/dist/libs/countup.js/dist/countUp.js"></script>
  42. <h1 data-countup>30000</h1>
  43. <h1 data-countup='{"useEasing": false}'>30000</h1>
  44. ```
  45. ## Use grouping
  46. Disable grouping using `"useGrouping": false`. Grouping is set to `true` by default and the default group separator is a comma.
  47. ```html code example
  48. <script src="$TABLER_CDN/dist/libs/countup.js/dist/countUp.js"></script>
  49. <h1 data-countup>30000</h1>
  50. <h1 data-countup='{"useGrouping": false}'>30000</h1>
  51. ```
  52. ## Separator
  53. You can change the default comma group separator using `separator` and specifying the one you wish to use.
  54. ```html code example
  55. <script src="$TABLER_CDN/dist/libs/countup.js/dist/countUp.js"></script>
  56. <h1 data-countup>3000000</h1>
  57. <h1 data-countup='{"separator":" "}'>3000000</h1>
  58. <h1 data-countup='{"separator":"-"}'>3000000</h1>
  59. <h1 data-countup='{"separator":":"}'>3000000</h1>
  60. ```
  61. ## Decimal separator
  62. You can also change the decimal separator which is set to a full stop by default. To do it, use `decimal` and specify the decimal separator of your choice.
  63. ```html code example
  64. <script src="$TABLER_CDN/dist/libs/countup.js/dist/countUp.js"></script>
  65. <h1 data-countup='{"decimalPlaces":2}'>3.12</h1>
  66. <h1 data-countup='{"decimalPlaces":2,"decimal":".."}'>3.12</h1>
  67. <h1 data-countup='{"decimalPlaces":2,"decimal":"^"}'>3.12</h1>
  68. <h1 data-countup='{"decimalPlaces":2,"decimal":":"}'>3.12</h1>
  69. ```
  70. ## Prefix
  71. Set the countup prefix using `prefix` and specifying the prefix you want to add, for instance a currency symbol.
  72. ```html code example
  73. <script src="$TABLER_CDN/dist/libs/countup.js/dist/countUp.js"></script>
  74. <h1 data-countup='{"prefix":"$"}'>30000</h1>
  75. <h1 data-countup='{"prefix":"€"}'>30000</h1>
  76. <h1 data-countup='{"prefix":"£"}'>30000</h1>
  77. ```
  78. ## Suffix
  79. Set the countup suffix using `suffix`.
  80. ```html code example
  81. <script src="$TABLER_CDN/dist/libs/countup.js/dist/countUp.js"></script>
  82. <h1 data-countup='{"suffix":"%"}'>30</h1>
  83. <h1 data-countup='{"suffix":"‰"}'>30</h1>
  84. <h1 data-countup='{"suffix":"k"}'>30</h1>
  85. ```
  86. Of course you can mix all of these:
  87. ```html code example
  88. <script src="$TABLER_CDN/dist/libs/countup.js/dist/countUp.js"></script>
  89. <h1 data-countup='{"duration":6,"startVal":12345,"decimalPlaces":2,"separator":" ","prefix":"$"}'>64547834.76</h1>
  90. ```
  91. For more advanced features of countups, click [**here**](https://inorganik.github.io/countUp.js/) and see what more you can do.