html倒计时动画,js+css3倒计时动画特效

这段JavaScript代码实现了一个DOM元素的动画效果,并在动画结束后重置元素状态。通过事件监听器处理动画的开始和结束,利用CSS类切换实现动画效果。点击重播按钮可以再次运行动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

js代码

const nums = document.querySelectorAll('.nums span');

const counter = document.querySelector('.counter');

const finalMessage = document.querySelector('.final');

const replay = document.getElementById('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 penultimate = nums.length - 1;

num.addEventListener('animationend', (e) => {

if(e.animationName === 'goIn' && idx !== penultimate){

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();

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值