display
property cannot be animated, as recommended by Michael G you can utilize a transition on the opacity
property instead. A more scalable approach is to apply a class to the element and use css
for managing style updates, especially if there are potential future style modifications to be made.
const submitButton = document.querySelector('#submit-button')
const load = document.querySelector('#load')
submitButton.addEventListener('click', () => {
load.classList.add("active");
setTimeout(() => {
load.classList.remove("active");
}, 3000)
})
body {
background-color: #313038;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.button {
padding: 10px 13px;
color: white;
background-color: transparent;
transition: 0.1s;
border: 1px solid #fff;
border-radius: 5px;
cursor: pointer;
}
.button:hover {
background-color: #fff;
transition: 0.1s;
color: black;
}
.lds-roller {
display: inline-block;
transition: 0.5s linear;
margin-top: 100px;
width: 80px;
height: 80px;
opacity: 0;
}
.lds-roller.active {
opacity: 1;
}
.lds-roller div {
animation: lds-roller 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
transform-origin: 40px 40px;
}
.lds-roller div:after {
content: " ";
display: block;
position: absolute;
width: 7px;
height: 7px;
border-radius: 50%;
background: #fff;
margin: -4px 0 0 -4px;
}
/* The rest of the CSS styles remain unchanged */
@keyframes lds-roller {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button type='submit' class='button' id='submit-button'>Submit</button>
<div id='load' class="lds-roller">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</body>
</html>