Categories
CSS JQuery 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!