How to apply animations and effects to websites using a combination of JavaScript and CSS.
JavaScript + jQuery effects
animation
transition
<div></div>
div { background-color: darkred; transition: all .5s linear; } .is-clicked { background-color: orange; }
$('div').on('click', function (e) { $(this).toggleClass('is-clicked'); });
div { height: 200px; width: 200px; } .is-clicked { animation: spin 1s ease; } @keyframes spin { 0% { transform: rotate(0); } 100% { transform: rotate(360deg); } }
$('div').on('click', function (e) { $(this).addClass('is-clicked'); });
Animations & transitions dispatch events that our JS can respond to:
animationstart
animationiteration
animationend
transitionend
$('div').on('click', function (e) { $(this).addClass('is-clicked'); }); $('div').on('animationend', function (e) { $(this).removeClass('is-clicked'); });
Treat it the same as any other HTML—just add classes, transitions & animations
<svg viewBox="0 0 200 200" width="200" height="200"> <circle fill="midnightblue" cx="100" cy="100" r="90" /> </svg>
circle { transition: all 1s ease; } .is-clicked { fill: purple; }
$('svg').on('click', function (e) { $('circle').toggleClass('is-clicked'); });