<div class = "animate__animated animate__fadeInUp animate__fast" role = "alert">
<strong>${alertmsg}</strong> ${msg}
<button type = "button" class = "btn-close" aria-label = "Close"></button>
</div>
как сделать так, чтобы оно исчезало после нажатия кнопки закрытия?
если бы я включил класс animate__fadeInUp и animate__fadeOutUp, он просто исчезнет.






Вы можете добиться эффекта затухания, используя это:
<script>
document.querySelector('.btn-close').addEventListener('click', function() {
const alertDiv = document.getElementById('alert');
alertDiv.classList.remove('animate__fadeInUp');
alertDiv.classList.add('animate__fadeOutUp');
alertDiv.addEventListener('animationend', () => {
alertDiv.remove();
});
});
</script>
Я считаю, что для достижения желаемого эффекта вы могли бы просто добавлять и удалять классы, используя обычные манипуляции с DOM, верно?
Вот пример,
<--! First your could just add id's to both the div and button -->
<div id = "alertContainer" class = "animate__animated animate__fadeInUp animate__fast" role = "alert">
<strong>${alertmsg}</strong> ${msg}
<button type = "button" id = "closeButton" class = "btn-close" aria-label = "Close"></button>
</div>
<script>
const alertContainer = document.getElementById('alertContainer');
const closeButton = document.getElementById('closeButton');
// Later in js, you could just remove the fadeIn animation and add fadeOut
closeButton.addEventListener('click', function() {
alertContainer.classList.remove('animate__fadeInUp');
alertContainer.classList.add('animate__fadeOutUp');
// Wait for the animation to complete before removing the alert from the DOM
setTimeout(function() {
alertContainer.remove();
}, 1000);
});
</script>
Позвольте мне знать, если это помогает
Ваше здоровье
Этого можно достичь, используя это:
<!DOCTYPE html>
<html>
<head>
<title>Alert Message</title>
<style>
@keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
.animate__fadeOut {
animation-name: fadeOut;
animation-duration: 2s;
animation-fill-mode: forwards;
}
</style>
</head>
<body>
<script>
let alertmsg = "Warning!";
let msg = "This is a warning message.";
let alertHTML = `
<div id = "alert" class = "animate__animated animate__fadeInUp animate__fast" role = "alert">
<strong>${alertmsg}</strong> ${msg}
<button id = "closeButton" type = "button" class = "btn-close" aria-label = "Close">Close</button>
</div>
`;
document.body.innerHTML += alertHTML;
document.getElementById('closeButton').addEventListener('click', function() {
document.getElementById('alert').classList.add('animate__fadeOut');
});
</script>
</body>
</html>
Вы можете использовать JavaScript или jQuery, чтобы добавить класс animate__fadeOutUp к элементу при нажатии кнопки.
Вот пример:
<div class = "animate__animated animate__fadeInUp animate__fast" role = "alert" id = "alertBox">
<strong>${alertmsg}</strong> ${msg}
<button type = "button" class = "btn-close" aria-label = "Close" onclick = "fadeOutAlert()"></button>
</div>
JS::
<script>
function fadeOutAlert() {
var alertBox = document.getElementById('alertBox');
alertBox.classList.remove('animate__fadeInUp');
alertBox.classList.add('animate__fadeOutUp');
}
</script>
jQuery::
$('.btn-close').click(function() {
$('#alertBox').removeClass('animate__fadeInUp').addClass('animate__fadeOutUp');
});
Обязательно включите библиотеку jQuery, если вы решите использовать версию jQuery.
Вы должны использовать FadeIn при отображении элемента и использовать Fadeout при его сокрытии. Только по одному. Что-то вроде переключателя, но опять же, это зависит от стилей, добавленных в файл Cals. Если вы можете создать код или скрипку, это может лучше помочь в вашем случае.