DEV Community

Cover image for Advance Hover Animation
Stackfindover
Stackfindover

Posted on • Updated on

Advance Hover Animation

Hello guys in this tutorial we will create Advance Hover Animation using HTML CSS & JavaScript

A hover animation occurs when the mouse hovers over the part, and therefore the element responds with motion or a transition. It’s used to highlight key items on a web page and it’s a good way to enhance your site’s interactivity. It’s used to highlight key items on a web page and it’s an effective way to enhance your site’s interactivity.

First, we need to create two files index.html and style.css then we need to do code for it.

Advance Hover Animation Step:1

Add below code inside index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Hover Animation JavaScript</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="style.css" />
    <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans&display=swap" rel="stylesheet">
  </head>
  <body>
    <div class="center-center">
      <button class="button fill" cursor="link">
        <span>Hover me</span>
      </button>
    </div>
    <script type="text/javascript">
      const $ = (a, b = document) => b.querySelectorAll(a);
      $('.button').forEach(el => el.addEventListener('mousemove', function(e) {
        const pos = this.getBoundingClientRect();
        const mx = e.clientX - pos.left - pos.width/2; 
        const my = e.clientY - pos.top - pos.height/2;

        this.style.transform = 'translate('+ mx * 0.15 +'px, '+ my * 0.3 +'px)';
        this.style.transform += 'rotate3d('+ mx * -0.1 +', '+ my * -0.3 +', 0, 12deg)';
        this.children[0].style.transform = 'translate('+ mx * 0.025 +'px, '+ my * 0.075 +'px)';
      }));
      $('.button').forEach(el => el.addEventListener('mouseleave', function() {
        this.style.transform = 'translate3d(0px, 0px, 0px)';
        this.style.transform += 'rotate3d(0, 0, 0, 0deg)';
        this.children[0].style.transform = 'translate3d(0px, 0px, 0px)';
      }));
    </script>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Advance Hover Animation Step:2

Then we need to add code for style.css which code I provide the below screen.

* {
    padding: 0;
    margin: 0;
    font-family: 'IBM Plex Sans', sans-serif;
}
body {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    background: #f1f2f3;
}

.button {
    background-color: #0f62fe;
    border: 1px solid transparent;
    color: #fff;
    cursor: pointer;
    outline: 0;
    display: block;
    font-size: 18px;
    width: 100%;
    text-align: center;
    padding: 10px 20px;
    box-shadow: 0 10px 30px rgba(65, 72, 86, 0.1);
    transition: transform 0.1s linear, color 0.1s linear, background 0.15s linear;
    transform-style: preserve-3d;
    z-index: 10000;
    position: relative;
}
Enter fullscreen mode Exit fullscreen mode

Advance Hover Animation video output:

Advance Hover Animation codepen output:

How To Create Responsive Landing Page In 2021

Top comments (0)