let me explain what i want to achieve.
I would like to show an animation only once during the session of an user. The animation can start only if the person quit is browser and comeback the the website.
If the person refresh or open the url in another tab the animation dosen't start again.
So, the animation is pretty simple, I have a two container taking the full height of the viewport.
The first one fade-out after 2s and let the place for the second container.
The goal is to keep that final state for the entire visit of an user.
The reason is to not show the animation multiple time so the experience will not become annoying.
Here the html and css
body {
margin: 0;
padding: 0;
}
.container {
font-size: 16px;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
color: #fff;
}
.first-container {
background-color: #21bab3;
animation: fade-out 2s ease-in forwards;
animation-delay: 1s;
}
.second-container {
background-color: tomato;
}
h1 {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
font-size: 10vw;
text-transform: uppercase;
}
/*ANIAMTION*/
@keyframes fade-out {
from {
opacity: 1;
height: 100vh;
}
to {
opacity: 0;
height: 0vh;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
</head>
<body>
<div class="container first-container"><h1>first</h1></div>
<div class="container second-container"><h1>second</h1></div>
<script src="main.js"></script>
</body>
</html>
I try to look how to maybe set the animation in a sessionStorage, but i didn't find answer.
I would like to achieve it without external library.
Thanks in advance
****EDIT*****
after a hint I try something with session storage but cant seem to exactly know what to do.
Im checking if a session key exist, if its null Im adding a class to the container I want to animate and setting a value the the session key.
After i check if the session key is true and if is true i remove the class that animated the container.
but now nothing happen
var firstContainer = document.querySelector(".first-container");
var secondContainer = document.querySelector(".second-container");
if (localStorage.getItem("animationPlayedOnce") === null) {
firstContainer.classList.add("animated");
sessionStorage.setItem("animationPlayedOnce", "true");
}
if (sessionStorage.getItem("animationPlayedOnce", "true")) {
firstContainer.classList.remove("animated");
}
body {
margin: 0;
padding: 0;
}
.container {
font-size: 16px;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
color: #fff;
}
.first-container {
background-color: #21bab3;
}
.animated {
animation: fade-out 2s ease-in forwards;
animation-delay: 1s;
}
.second-container {
background-color: tomato;
}
h1 {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
font-size: 10vw;
text-transform: uppercase;
}
/*ANIAMTION*/
@keyframes fade-out {
from {
opacity: 1;
height: 100vh;
}
to {
opacity: 0;
height: 0vh;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
</head>
<body>
<div id="animated" class="container first-container animated">
<h1>first</h1>
</div>
<div class="container second-container"><h1>second</h1></div>
<script src="main.js"></script>
</body>
</html>