Categories
Web development

CSS/JS Eye Follows Mouse Cursor + Blink on Hover

CSS/JS Eye Animation

Demo:

*to see the CSS/JavaScript Eye Animation on the website click here.

To create the CSS/JavaScript Eye Animation follow the steps below and watch the video tutorial:

  1. Add HTML
  2. Add CSS
  3. Add JavaScript

Step1.

Add HTML

Create the container with the eyeball, iris, and eyelid:

<div class="container">
  <div class="eyeBall">
    <div class="iris"></div>
  </div>
  <div class="eyeLid"></div>
  <div class="lid"></div>
</div>

Step2.

Add CSS

Set the colour and the position of the background and elements:

body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.container {
  position: relative;
  cursor: pointer;
}

Style the eye:

.iris {
  position: absolute;
  width: 70px;
  height: 70px;
  border: 5px solid #333;
  background-color: #0077b6;
  border-radius:50%;
  left:40px;  
  top:30px;
}

.iris:before {
  content:"";
  position: absolute;
  background-color: #333;
  border-radius:50%;
  width:40px;
  height: 40px;
  top:22%;
  left:22%;
}

.iris:after {
  content:"";
  position: absolute;
  background-color: rgba(255,255,255,0.4);
  border-radius: 50%;
  width:15px;
  height:15px;
  top:35%;
  left:65%;
  box-shadow: -35px 20px rgba(255,255,255,0.4);
}

.eyeBall {
  position: relative;
  width: 150px;
  height: 150px;
  background-color: transparent;
  border: 5px solid #333;
  border-radius:100% 0;
  box-shadow: inset 5px 5px 5px rgba(0,0,0,0.3);
  transform: rotate(45deg);
  top:-10px;
  z-index:1;
  overflow: hidden;
}

.eyeLid {
  position: absolute;
  border-top: 5px solid #333;
  border-left: 5px solid #333;
  border-radius:100% 0;
  width: 150px;
  height: 150px;
  top:-30px;
  left:3px;
  transform: rotate(45deg);
  transition: .2s;
  z-index:10;
}

.lid {
  position: absolute;
  z-index:9;
  background-color: #fff;
  width: 295px;
  height: 270px;
  border-radius:50%;
  top:-248px;
  left:-68px;
  transition: .2s;
}

To make the eye blink add transition on hover:

.container:hover .eyeLid {
  transform: rotate(-45deg) rotateX(160deg);
}

.container:hover .lid {
  transform: translateY(90px);
}

Step3.

Add JavaScript

const eye = document.querySelector('.iris');
window.addEventListener('mousemove', (event) => {
const x = -(window.innerWidth / 2 - event.pageX) / 35;
const y = -(window.innerHeight / 2 - event.pageY) / 35;
eye.style.transform = `rotate(-45deg) translateY(${y}px)  translateX(${x}px)`;         
        }); 

Watch also the video tutorial:

Enjoy coding!

Read also:

CSS & jQuery Eye Animation

CSS & JavaScript Image Magnifier Glass

CSS & JavaScript Bee Progress Bar

Categories
Web development

CSS & jQuery Eye Animation

CSS eye follow animation

Demo:

*to see the animation on the website click here.

To create the CSS & jQuery Eye Animation watch the video tutorial and follow the steps below:

  1. Add HTML
  2. Add CSS
  3. Add CSS Animation
  4. Add jQuery/ JavaScript

Step 1.

Add HTML

<div class="container">
  <div class="ball">
    <div class="iris"></div>
    </div>
  <div class="shadow"></div>
  </div>

Step 2.

Add CSS

Set the colour and the position of the background and elements:

body {
  background-color: #F5B7B1;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

Style the eyeball and iris:

.container {
  position: relative;   
}

.ball {
  overflow: hidden;
  width: 180px;
  height: 180px;
  border-radius: 50%;
  position: relative;
  background: radial-gradient(circle, rgba(255,255,255,1) 31%, rgba(255,253,253,1) 99%, rgba(218,218,218,1) 100%);
  box-shadow:inset -5px -5px 5px rgba(0,0,0,0.07);
  animation: bounceball alternate infinite 400ms 40ms ease-in-out;
}
.iris {
  width: 40%;
  height: 40%;
  left: 30%;
  top: 30%;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 50%, #9ACD32 0%, #00FF00 30%, #008000 100%);
  position: absolute;
}
.iris:before {
  content: "";
  position: absolute;
  width: 35%;
  height: 35%;
  border-radius: 50%;
  top: 30%;
  left: 30%;
  background-color: black;
}

.iris:after {
  content: "";
  position: absolute;
  width: 31.25%;
  height: 31.25%;
  border-radius: 50%;
  top: 20%;
  left: 20%;
  background-color: rgba(255, 255, 255, 0.43);
}

Add the shadow:

.shadow {
  position: absolute;
  width:160px;
  height: 60px;
  border-radius:50%;
  background-color: rgba(0,0,0,0.09);
  top:190px;
  left:14px;
  animation: bounceball alternate infinite 400ms 40ms ease-in-out;
}
CSS eye follow animation

Step 3.

Add CSS Animation

For the eyeball:

@keyframes bouncebody { 
  to { transform: scaleX(1.03) scaleY(0.97); } 
}

Step 4.

Add JavaScript/jQuery

$(function() {
  var midWidth = Math.floor($(window).width() / 2);
  var midHeight = 100;
  var mousePos = {
    x: midWidth,
    y: midHeight
  };
  $(document).mousemove(function(event) {
    mousePos.x = event.pageX;
    mousePos.y = event.pageY;
    var transX = mousePos.x - midWidth;
    var transY = mousePos.y - midHeight;
    var skewX = 0;
    if (transX > -30 && transX < 30 && transY < 0) {
      skewX = transX / 2;
    } else if (transX > -30 && transX < 30 && transY >= 0) {
      skewX = -(transX / 2);
    } else if (transX > 30) {
      skewX = -(transY / 2);
    } else if (transX < -30) {
      skewX = transY / 2;
    }
    if (transX >= 30) {
      transX = 30;
    } else if (transX <= -30) {
      transX = -30;
    }
    if (transY >= 30) {
      transY = 30;
    } else if (transY <= -30) {
      transY = -30;
    }

    if (skewX >= 15) {
      skewX = 15;
    } else if (skewX <= -15) {
      skewX = -15;
    }
    $('.iris').css({
      "transform": "translateX(" + transX + "px) translateY(" + transY + "px) skewX(" + skewX + "deg) skewY(2deg)"
    });

  });

});

Enjoy coding!

Read also:

CSS/JS Eye Follows Mouse Cursor + Blink on Hover

Div (circle) follows the cursor

CSS & JavaScript Image Magnifier Glass