<!-- data-duration=表示時間(ミリ秒) --> <div class="bnto-toast" data-bnto-toast data-duration="3000"> <button type="button" class="tg">表示する</button> <div class="box" aria-live="polite"></div> </div>
.bnto-toast { --c1: hsl(202 73% 50%); /* アクセント色 */ position: relative; min-height: 150px; text-align: center; } .bnto-toast .tg { background: var(--c1); color: #fff; border: none; border-radius: 999px; padding: 9px 20px; font: inherit; font-weight: 800; cursor: pointer; } /* デモはカード内に表示。ページ全体に出すなら position:fixed; left:auto; right:16px; bottom:16px; に */ .bnto-toast .box { position: absolute; left: 0; right: 0; bottom: 0; display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; } .bnto-toast .it { display: flex; align-items: center; gap: 9px; background: #1D2A36; color: #fff; font-size: 13px; font-weight: 700; padding: 10px 16px; border-radius: 12px; box-shadow: 0 10px 24px rgba(0,0,0,.22); /* フェード+スライドイン */ opacity: 0; transform: translateY(14px); transition: opacity .3s, transform .3s; pointer-events: auto; } .bnto-toast .it.show { opacity: 1; transform: none; } .bnto-toast .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--c1); }
// data-bnto-toast を自動検出して初期化(複数設置OK・二重実行OK) (function () { document.querySelectorAll('[data-bnto-toast]').forEach(function (host) { if (host.dataset.bntoInit) return; host.dataset.bntoInit = '1'; var box = host.querySelector('.box'); host.querySelector('.tg').addEventListener('click', function () { var old = box.querySelector('.it'); // このデモでは1つだけ表示 if (old) old.remove(); var it = document.createElement('div'); it.className = 'it'; it.innerHTML = '<span class="dot"></span>保存しました'; box.appendChild(it); void it.offsetWidth; // 一度レイアウトさせてから .show it.classList.add('show'); setTimeout(function () { it.classList.remove('show'); setTimeout(function () { it.remove(); }, 350); }, parseInt(host.dataset.duration, 10) || 3000); }); }); })();
使い方のコツ
表示時間は data-duration(ミリ秒)で変更できます。画面右下に出したいときは .box を position:fixed; right:16px; bottom:16px; left:auto; に変えるだけです。メッセージ文はJSの innerHTML の部分を書き換えてください。