Note - Double Click to Copy Code Contact Us!

Animated-Countdown using html css js

Tech Doubility
Animated-Countdown

HTML Code
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <link rel="stylesheet" href="style.css" />
  7. <title>Animated Countdown</title>
  8. </head>
  9. <body>
  10. <div class="counter">
  11. <div class="nums">
  12. <span class="in">3</span>
  13. <span>2</span>
  14. <span>1</span>
  15. <span>0</span>
  16. </div>
  17. <h4>Get Ready</h4>
  18. </div>
  19.  
  20. <div class="final">
  21. <h1>GO</h1>
  22. <button id="replay">
  23. <span>Replay</span>
  24. </button>
  25. </div>
  26. <script src="script.js"></script>
  27. </body>
  28. </html>
CSS Code
  1. @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
  2.  
  3. * {
  4. box-sizing: border-box;
  5. }
  6.  
  7. body {
  8. font-family: 'Roboto', sans-serif;
  9. margin: 0;
  10. height: 100vh;
  11. overflow: hidden;
  12. }
  13.  
  14. h4 {
  15. font-size: 20px;
  16. margin: 5px;
  17. text-transform: uppercase;
  18. }
  19.  
  20. .counter {
  21. position: fixed;
  22. top: 50%;
  23. left: 50%;
  24. transform: translate(-50%, -50%);
  25. text-align: center;
  26. }
  27.  
  28. .counter.hide {
  29. transform: translate(-50%, -50%) scale(0);
  30. animation: hide 0.2s ease-out;
  31. }
  32.  
  33. @keyframes hide {
  34. 0% {
  35. transform: translate(-50%, -50%) scale(1);
  36. }
  37.  
  38. 100% {
  39. transform: translate(-50%, -50%) scale(0);
  40. }
  41. }
  42.  
  43. .final {
  44. position: fixed;
  45. top: 50%;
  46. left: 50%;
  47. transform: translate(-50%, -50%) scale(0);
  48. text-align: center;
  49. }
  50.  
  51. .final.show {
  52. transform: translate(-50%, -50%) scale(1);
  53. animation: show 0.2s ease-out;
  54. }
  55.  
  56. @keyframes show {
  57. 0% {
  58. transform: translate(-50%, -50%) scale(0);
  59. }
  60.  
  61. 30% {
  62. transform: translate(-50%, -50%) scale(1.4);
  63. }
  64.  
  65. 100% {
  66. transform: translate(-50%, -50%) scale(1);
  67. }
  68. }
  69.  
  70. .nums {
  71. color: #3498db;
  72. font-size: 50px;
  73. position: relative;
  74. overflow: hidden;
  75. width: 250px;
  76. height: 50px;
  77. }
  78.  
  79. .nums span {
  80. position: absolute;
  81. top: 50%;
  82. left: 50%;
  83. transform: translate(-50%, -50%) rotate(120deg);
  84. transform-origin: bottom center;
  85. }
  86.  
  87. .nums span.in {
  88. transform: translate(-50%, -50%) rotate(0deg);
  89. animation: goIn 0.5s ease-in-out;
  90. }
  91.  
  92. .nums span.out {
  93. animation: goOut 0.5s ease-in-out;
  94. }
  95.  
  96. @keyframes goIn {
  97. 0% {
  98. transform: translate(-50%, -50%) rotate(120deg);
  99. }
  100.  
  101. 30% {
  102. transform: translate(-50%, -50%) rotate(-20deg);
  103. }
  104.  
  105. 60% {
  106. transform: translate(-50%, -50%) rotate(10deg);
  107. }
  108.  
  109. 100% {
  110. transform: translate(-50%, -50%) rotate(0deg);
  111. }
  112. }
  113.  
  114. @keyframes goOut {
  115. 0% {
  116. transform: translate(-50%, -50%) rotate(0deg);
  117. }
  118.  
  119. 60% {
  120. transform: translate(-50%, -50%) rotate(20deg);
  121. }
  122.  
  123. 100% {
  124. transform: translate(-50%, -50%) rotate(-120deg);
  125. }
  126. }
  127.  
  128. #replay{
  129. background-color: #3498db;
  130. border-radius: 3px;
  131. border: none;
  132. color: aliceblue;
  133. padding: 5px;
  134. text-align: center;
  135. display: inline-block;
  136. cursor: pointer;
  137. transition: all 0.3s;
  138. }
  139.  
  140. #replay span{
  141. cursor: pointer;
  142. display: inline-block;
  143. position: relative;
  144. transition: 0.3s;
  145. }
  146.  
  147. #replay span:after{
  148. content: '\00bb';
  149. position: absolute;
  150. opacity: 0;
  151. top: 0;
  152. right: -20px;
  153. transition: 0.5s;
  154. }
  155.  
  156. #replay:hover span{
  157. padding-right: 25px;
  158. }
  159.  
  160. #replay:hover span:after{
  161. opacity: 1;
  162. right: 0;
  163. }
JS Code
  1. const nums = document.querySelectorAll('.nums span')
  2. const counter = document.querySelector('.counter')
  3. const finalMessage = document.querySelector('.final')
  4. const replay = document.querySelector('#replay')
  5.  
  6. runAnimation()
  7.  
  8. function resetDOM() {
  9. counter.classList.remove('hide')
  10. finalMessage.classList.remove('show')
  11.  
  12. nums.forEach((num) => {
  13. num.classList.value = ''
  14. })
  15.  
  16. nums[0].classList.add('in')
  17. }
  18.  
  19. function runAnimation() {
  20. nums.forEach((num, idx) => {
  21. const nextToLast = nums.length - 1
  22.  
  23. num.addEventListener('animationend', (e) => {
  24. if (e.animationName === 'goIn' && idx !== nextToLast) {
  25. num.classList.remove('in')
  26. num.classList.add('out')
  27. } else if (e.animationName === 'goOut' && num.nextElementSibling) {
  28. num.nextElementSibling.classList.add('in')
  29. } else {
  30. counter.classList.add('hide')
  31. finalMessage.classList.add('show')
  32. }
  33. })
  34. })
  35. }
  36.  
  37. replay.addEventListener('click', () => {
  38. resetDOM()
  39. runAnimation()
  40. })

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.