39 lines
1.2 KiB
JavaScript
39 lines
1.2 KiB
JavaScript
const Toast = {
|
|
_container: null,
|
|
|
|
_init() {
|
|
if (this._container) return;
|
|
this._container = document.createElement('div');
|
|
this._container.className = 'toast-container';
|
|
document.body.appendChild(this._container);
|
|
},
|
|
|
|
show(message, type = 'info', duration = 3000) {
|
|
this._init();
|
|
const toast = document.createElement('div');
|
|
toast.className = `toast ${type}`;
|
|
toast.textContent = message;
|
|
toast.style.cursor = 'pointer';
|
|
toast.addEventListener('click', () => {
|
|
toast.style.opacity = '0';
|
|
toast.style.transform = 'translateY(20px)';
|
|
toast.style.transition = 'all 0.2s ease';
|
|
setTimeout(() => toast.remove(), 200);
|
|
});
|
|
this._container.appendChild(toast);
|
|
|
|
if (duration > 0) {
|
|
setTimeout(() => {
|
|
if (toast.parentNode) {
|
|
toast.style.opacity = '0';
|
|
toast.style.transform = 'translateY(20px)';
|
|
toast.style.transition = 'all 0.3s ease';
|
|
setTimeout(() => toast.remove(), 300);
|
|
}
|
|
}, duration);
|
|
}
|
|
}
|
|
};
|
|
|
|
window.Toast = Toast;
|