skip to Main Content

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


  1. Chosen as BEST ANSWER

    Well , after posting the question , i myself came up with a mind-blowing solution which uses variable property .

    * {
        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 {
        animation: strech 1.5s ease-in-out 1 forwards;
    }
    
    @keyframes strech {
        0% {
            scale: .7;
            --val: .7;
        }
    
        20% {
            scale: 1;
            --val: 1;
        }
    
        40% {
            scale: .9;
            --val: .9;
        }
    
        60% {
            scale: 1;
            --val: 1;
        }
    
        80% {
            scale: .95;
            --val: .95;
        }
    
        100% {
            scale: 1;
            --val: 1;
        }
    }
    
    @keyframes shrink {
        0% {
            scale: var(--val);
        }
    
        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 1"></div>
    
    </body>
    </html>


  2. The reason it snaps back is because of the scale property on container:

    .container {
        scale: .7;
    }
    

    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.

    Login or Signup to reply.
  3. You mispelled the word stretch and you don’t need the keyframes shrink.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
     *{
        box-sizing: border-box;
        margin: 0;
    }
    
    body{
        height: 100vh;
        width: 100vw;
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
    }
    
    .container{
        transition:.7s;
        margin: 10px;
        height: 60vh;
        width: 70vw;
        background-color: rgb(42, 116, 228);
        scale: .7;
        animation: shrink .5s 1s;
        }
    
    .container:hover{
        scale: 1;
        animation: stretch 1.5s ease-in-out 1;
    }
    
    @keyframes strech {
        0%{
            scale: .7;
        }
        20%{
            scale: 1;
        }
      
        80%{
            scale: .95;
        }
        100%{
            scale:1;
        }
    }
        </style>
    </head>
    <body>
      <div class="container"></div>
    </body>
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search