Animation + Transition

by Zeaklous

HTML

<div class="ball animated"></div>

CSS

body {margin: 100px;}

@-webkit-keyframes pulsate {
    0% {
        -webkit-transform: scale(0.7);
    }    
    50% {
        -webkit-transform: scale(0.8);
    }
    100% {
        -webkit-transform: scale(0.7);
    }
}

.ball {
    background: blue;
    width: 100px;
    height: 100px;
    border-radius: 50%;    
    -webkit-transform: scale(0.7);
    -webkit-transform-origin: center center; 
    transition: all 1s;
}
.ball.animated {
  -webkit-animation-duration: 1600ms; 
  -webkit-animation-name: pulsate;
  -webkit-animation-iteration-count: infinite;
  -webkit-animation-direction: alternate;
  -webkit-animation-timing-function: ease-in-out;
}

JavaScript

var ball = document.getElementsByClassName('ball')[0],
    pfx = ["webkit", "moz", "MS", "o", ""],
    hovered = false;

function AnimationListener() {
    if(hovered)
    { 
      ball.classList.remove('animated'); 
      ball.style.webkitTransform = 'scale(2)';
      ball.style.transform = 'scale(2)';
    }
}

function TransitionListener() {
  if(!hovered)
  {
    ball.classList.add('animated');
  }
}

function PrefixedEvent(element, type, callback) {
	for (var p = 0; p < pfx.length; p++) {
		if (!pfx[p]) type = type.toLowerCase();
		element.addEventListener(pfx[p]+type, callback, false);
	}
}

PrefixedEvent(ball, "AnimationIteration", AnimationListener);

ball.onmouseover = function() {
  hovered = true;
}
ball.onmouseout = function() {
  hovered = false;
  PrefixedEvent(ball, "TransitionEnd", TransitionListener);
  ball.style.webkitTransform = 'scale(.7)';
  ball.style.transform = 'scale(.7)';  
}