Вы можете сделать это многими способами. Вот несколько примеров.
Использование флага и setTimeout
Это означает, что ваш скрипт должен знать продолжительность.
const cells = document.querySelectorAll('.cell');
let animationComplete = false;
setTimeout(() => animationComplete = true, 5000);
cells.forEach(cell => cell.addEventListener('click', (e) => {
if (animationComplete) {
console.log("You can click because the animation is over");
}
}));
<div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><style>.cell{padding: 20px; background: red; display: inline-block; animation: fadeIn 5s;}@keyframes fadeIn{from{opacity: 0}to{opacity: 1}}</style>
Использование setTimeout и ожидание перед настройкой прослушивателя событий
Это означает, что ваш сценарий должен знать продолжительность.
const cells = document.querySelectorAll('.cell');
setTimeout(() => {
cells.forEach(cell => cell.addEventListener('click', (e) => {
console.log("You can click because the animation is over");
}));
}, 5000);
<div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><style>.cell{padding: 20px; background: red; display: inline-block; animation: fadeIn 5s;}@keyframes fadeIn{from{opacity: 0}to{opacity: 1}}</style>
Использование флага и события animationend
Не нужно знать продолжительность! Измените его в CSS, и JS будет следовать.
const cells = document.querySelectorAll('.cell');
let animationComplete = false;
cells[0].addEventListener(whichAnimationEvent(), () => animationComplete = true);
cells.forEach(cell => cell.addEventListener('click', (e) => {
if (animationComplete) {
console.log("You can click because the animation is over");
}
}));
// The name of the event depends on the browser
function whichAnimationEvent(){
var t, el = document.createElement("fakeelement");
var animations = {
"animation" : "animationend",
"OAnimation" : "oAnimationEnd",
"MozAnimation" : "animationend",
"WebkitAnimation": "webkitAnimationEnd"
}
for (t in animations){
if (el.style[t] !== undefined){
return animations[t];
}
}
}
<div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><style>.cell{padding: 20px; background: red; display: inline-block; animation: fadeIn 5s;}@keyframes fadeIn{from{opacity: 0}to{opacity: 1}}</style>
Использование события animationend
и ожидание перед настройкой прослушивателя события
Не нужно также знать продолжительность .
const cells = document.querySelectorAll('.cell');
cells[0].addEventListener(whichAnimationEvent(), () => {
cells.forEach(cell => cell.addEventListener('click', (e) => {
console.log("You can click because the animation is over");
}));
});
// The name of the event depends on the browser
function whichAnimationEvent(){
var t, el = document.createElement("fakeelement");
var animations = {
"animation" : "animationend",
"OAnimation" : "oAnimationEnd",
"MozAnimation" : "animationend",
"WebkitAnimation": "webkitAnimationEnd"
}
for (t in animations){
if (el.style[t] !== undefined){
return animations[t];
}
}
}
<div class="cell"></div><div class="cell"></div><div class="cell"></div><div class="cell"></div><style>.cell{padding: 20px; background: red; display: inline-block; animation: fadeIn 5s;}@keyframes fadeIn{from{opacity: 0}to{opacity: 1}}</style>