- var banners = [{ icon: 'alert-circle-outline', class: 'error', message: 'Oops! Something went wrong!' }, {icon: 'checkmark-circle-outline', class:'success', message: 'Everything was fine!'},{icon: 'info-outline', class:'info', message: 'Here is some useful information'}];
.banners-container
.banners
each banner in banners
.banner(class=banner.class)
.banner-icon
i(
data-eva=banner.icon,
data-eva-fill="#ffffff",
data-eva-height="48",
data-eva-width="48"
)
.banner-message= banner.message
.banner-close(onclick="hideBanners()")
i(data-eva="close-outline", data-eva-fill="#ffffff")
button.show-banner(onclick="showBanner('.banner.error')") Show Error
button.show-banner(onclick="showBanner('.banner.success')") Show Success
button.show-banner(onclick="showBanner('.banner.info')") Show Info
script(src="https://unpkg.com/eva-icons", onload="eva.replace()")
View Compiled
@import url("https://fonts.googleapis.com/css?family=Montserrat:400,400i,700");
$overshoot: 10%;
$color-error: #ed1c24;
$color-success: #10c15c;
$color-info: #0b22e2;
:root {
font-family: "Montserrat";
}
html,
body {
margin: 0;
height: 100%;
}
body {
display: flex;
align-items: center;
justify-content: center;
}
i {
color: inherit;
}
.banners-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
}
.banner {
color: white;
font-weight: 700;
padding: 2rem;
display: flex;
flex-direction: row;
align-items: center;
.banner-message {
flex: 1;
padding: 0 2rem;
}
.banner-close {
display: flex;
align-items: center;
justify-content: center;
padding: 0.5rem;
border-radius: 4px;
cursor: pointer;
transition: background 0.3s;
&:hover {
background: rgba(0, 0, 0, 0.12);
}
}
}
@mixin banner($background) {
background: $background;
&::after {
background: $background;
}
}
.banner {
&.success {
@include banner($color-success);
}
&.error {
@include banner($color-error);
}
&.info {
@include banner($color-info);
}
}
.banner::after {
content: "";
position: absolute;
height: $overshoot;
width: 100%;
bottom: 100%;
left: 0;
}
.banner:not(.visible) {
display: none;
transform: translateY(-100%);
}
.banner.visible {
box-shadow: 0 2px 2px 2px rgba(0, 0, 0, 0.12);
animation-name: banner-in;
animation-direction: forwards;
animation-duration: 0.6s;
animation-timing-function: ease-in-out;
animation-fill-mode: forwards;
animation-iteration-count: 1;
}
@keyframes banner-in {
0% {
transform: translateY(-100%);
}
50% {
transform: translateY($overshoot);
}
100% {
transform: translateY(0);
}
}
.show-banner {
appearance: none;
background: #ededed;
border: 0;
padding: 1rem 2rem;
border-radius: 4px;
cursor: pointer;
text-transform: uppercase;
margin: 0.25rem;
}
View Compiled
// Pssst, I've created a github package - https://github.com/brookesb91/dismissible
const showBanner = (selector) => {
hideBanners();
// Ensure animation plays even if the same alert type is triggered.
requestAnimationFrame(() => {
const banner = document.querySelector(selector);
banner.classList.add("visible");
});
};
const hideBanners = (e) => {
document
.querySelectorAll(".banner.visible")
.forEach((b) => b.classList.remove("visible"));
};
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.