<div class="messageContainer">
<p class="message"></p>
</div>
.messageContainer {
position: absolute;
left: 50%;
top: 50%;
transform: translateX(-50%) translateY(-50%);
width: 50ch;
min-height: 50vh;
padding: 1em;
background-color: #dddddd;
opacity: 0;
transition: opacity 2000ms ease-in-out;
transition-delay: 500ms;
}
.messageContainer.visible {
opacity: 1;
}
.message {
text-align: center;
line-height: 50vh;
}
.fade-in-out {
animation: fade-in-out;
animation-duration: 2500ms;
}
@keyframes fade-in-out {
0%, 100% {
opacity: 0;
}
50% {
opacity: 1;
}
}
const messages = [
'Hello, welcome to my tutorial',
'It seems you\'re new here',
'Don\'t forget to sign up to my mailing list',
]
let msg = document.querySelector('.message');
//Load in initial container
document.querySelector('.messageContainer').classList.add('visible');
//start sequence
setTimeout(changeMessage.bind(null, 0), 2500);
function changeMessage(index) {
if (index >= messages.length) { return; }
msg.classList.remove('fade-in-out');
msg.textContent = messages[index];
//Trigger reflow so page re-calculates itself
//and doesn't optimize away the adding and
//removing of the same class
void msg.offsetWidth;
msg.classList.add('fade-in-out');
setTimeout(changeMessage.bind(null, index + 1), 2500);
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.