デモ
See the Pen toast by logsuke (@design-list) on CodePen.
コード
<!-- Boxicons headタグ内へ -->
<link href='https://unpkg.com/boxicons@2.1.2/css/boxicons.min.css' rel='stylesheet'>
<main>
<div id="toast">
<p>message</p>
<i id="close" class='bx bx-x'></i>
</div>
<section id="section1" class="section">
<button id="button" class="button">toast</button>
</section>
</main>
/*=== Base ===*/
* {
margin: 0;
padding: 0;
box-sizing: border-box
}
:root {
--nav-height: 8rem;
--max-width: 1024px;
--container-padding: 0 4rem;
--gap: 2rem;
--bg-color: rgb(240, 240, 240);
--first-color: rgb(172, 245, 255);
}
/*=== Font ratio ===*/
html {
scroll-behavior: smooth;
font-size: 62.5%;
}
body {
font-size: 1.6rem;
}
ul {
list-style: none;
}
a {
text-decoration: none;
}
.section {
width: var(--max-width);
margin: 0 auto;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
gap: 10rem;
}
/* tool tip */
.button {
width: 150px;
height: 50px;
padding: .5rem 1rem;
background: var(--first-color);
border: none;
font-size: 2rem;
border-radius: .5rem;
cursor: pointer;
position: relative;
}
/* toast */
#toast {
position: absolute;
width: 250px;
height: 100px;
top: 50px;
right: 50px;
background: #ccc;
padding: 2rem 3rem;
border: 6px double #fff;
border-radius: 1rem;
display: flex;
justify-content: space-between;
align-items: center;
opacity: 0;
pointer-events: none;
user-select: none;
transform: translateX(10px);
transition: transform .3s, opacity .3s;
}
#toast.show {
opacity: 1;
transform: translateX(0);
pointer-events: auto;
user-select: auto;
}
#toast p {
color: #fff;
}
#toast i {
background: #fff;
padding: .5rem;
border-radius: 50%;
cursor: pointer;
}
const button = document.getElementById('button'),
toast = document.getElementById('toast'),
close = document.getElementById('close')
button.addEventListener('click', function () {
if (!toast.classList.contains('show')) {
toast.classList.add('show')
}
setTimeout(function () {
toast.classList.remove('show')
}, 3000)
})
close.addEventListener('click', function () {
toast.classList.remove('show')
})