Skip to content

Commit

Permalink
Create script.js
Browse files Browse the repository at this point in the history
  • Loading branch information
20240906book authored Sep 10, 2024
1 parent fcd29f0 commit c9ba442
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const nums = document.querySelectorAll('.nums span')
const counter = document.querySelector('.counter')
const finalMessage = document.querySelector('.final')
const replay = document.querySelector('#replay')

runAnimation()

function resetDOM() {
counter.classList.remove('hide')
finalMessage.classList.remove('show')

nums.forEach((num) => {
num.classList.value = ''
})

nums[0].classList.add('in')
}

function runAnimation() {
nums.forEach((num, idx) => {
const nextToLast = nums.length - 1

num.addEventListener('animationend', (e) => {
if (e.animationName === 'goIn' && idx !== nextToLast) {
num.classList.remove('in')
num.classList.add('out')
} else if (e.animationName === 'goOut' && num.nextElementSibling) {
num.nextElementSibling.classList.add('in')
} else {
counter.classList.add('hide')
finalMessage.classList.add('show')
}
})
})
}

replay.addEventListener('click', () => {
resetDOM()
runAnimation()
})

0 comments on commit c9ba442

Please sign in to comment.