I tried to add animation on both hover and unhover state but that jumps the <div/>
to its original state and then follows the animation.
What can be done to have a fluid animation on hovering and unhovering the <div/>
?
Any kind of JavaScript and CSS is acceptable, but please avoid GSAP libraries as I am not familiar with them.
* {
box-sizing: border-box;
margin: 0;
}
body {
height: 100vh;
width: 100vw;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
.container {
transition: .45s;
margin: 10px;
height: 60vh;
width: 70vw;
background-color: antiquewhite;
scale: .7;
animation: shrink .45s ease-in-out 1 forwards;
}
.container:hover {
scale: 1;
animation: strech 1.5s ease-in-out 1;
}
@keyframes strech {
0% {
scale: .7;
}
20% {
scale: 1;
}
40% {
scale: .9;
}
60% {
scale: 1;
}
80% {
scale: .95;
}
100% {
scale: 1;
}
}
@keyframes shrink {
20% {
scale: .9;
}
100% {
scale: .7;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container"></div>
</body>
</html>
3
Answers
Well , after posting the question , i myself came up with a mind-blowing solution which uses variable property .
The reason it snaps back is because of the scale property on container:
When the container reverts back to its default (unhovered) state, it goes to .7 immediately and then plays the animation. If you remove this line then I think you will get the result you desire.
You mispelled the word stretch and you don’t need the keyframes shrink.