/
githubmirror
/
50projects50days
Обзор
Документация
Войти
/
githubmirror
/
50projects50days
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
toast-notification/script.js
35 строк
842 B
Brad Traversy
Toast notification
03 ноя 2020, 23:34
03 ноя 2020, 23:34
96a5926
Код
Авторство
О чём код?
const button = document.getElementById('button') const toasts = document.getElementById('toasts') const messages = [ 'Message One', 'Message Two', 'Message Three', 'Message Four', ] const types = ['info', 'success', 'error'] button.addEventListener('click', () => createNotification()) function createNotification(message = null, type = null) { const notif = document.createElement('div') notif.classList.add('toast') notif.classList.add(type ? type : getRandomType()) notif.innerText = message ? message : getRandomMessage() toasts.appendChild(notif) setTimeout(() => { notif.remove() }, 3000) } function getRandomMessage() { return messages[Math.floor(Math.random() * messages.length)] } function getRandomType() { return types[Math.floor(Math.random() * types.length)] }