Animation How to - Handle CSS3 transition start and end events








Question

We would like to know how to handle CSS3 transition start and end events.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.7.1.js'></script>
<style type='text/css'>
div {<!-- w  w  w  .ja  v  a  2s .com-->
  width: 50px;
  height: 50px;
  background: #EEE;
  border-radius: 50px;
  box-shadow: 0 0 10px orange;
  transition: all 1s ease-in-out;
  -webkit-transition: all 1s ease-in-out;
  -moz-transition: all 1s ease-in-out;
  -o-transition: all 1s ease-in-out;
  -ms-transition: all 1s ease-in-out;
}

div.animate {
  margin-left: 150px;
  box-shadow: 0 0 30px orange;
}
</style>
<script type='text/javascript'>
$(function(){
    $('#trigger').on('mouseover',function(){
        $(this).toggleClass('animate');
        $('#debug').html('Animation started');
    });
    $('#trigger').on('webkitTransitionEnd moztransitionend transitionend oTransitionEnd', function () {
        $('#debug').html('Animation ended');
    });
});
</script>
</head>
<body>
  <div id='trigger'></div>
  <p id='debug'></p>
</body>
</html>

The code above is rendered as follows: