$Animation-Buttons

by mtambulut

HTML

<p><strong>Click</strong> a button to activate its animation.</p>
<div class='btn-container'>
  <button class='btn btn--shockwave is-active'>
    Shockwave
  </button>
  <button class='btn btn--jump'>
    Jump
  </button>
  <button class='btn btn--pulse'>
    Pulse
  </button>
  <button class='btn btn--blink'>
    Blink
  </button>
  <button class='btn btn--wiggle'>
    Wiggle
  </button>
  <button class='btn btn--wut'>
    Hatching Alien
  </button>
</div>

SCSS

$spacing__base: 1.6rem;
$easing: cubic-bezier(.33,.33,.33,1);
$background:  darken(#daecf3, 12);
$gradient_end: darken(#D7EDEC, 8);

@mixin afterBg {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  border-radius: 50%;
}

body {
    max-width: 600px;
    margin: $spacing__base * 3 auto;
    background: $background;
    font-family: tahoma, helvetica neue, helvetica, arial, sans-serif;
    background: -webkit-linear-gradient(to right, $background, $gradient_end);
    background: linear-gradient(to right, $background, $gradient_end);
    text-align: center;
   
}

.btn-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.btn {
  width: $spacing__base * 4;
  line-height: $spacing__base * 4;
  background: #fff;
  border-radius: 50%;
  text-align: center;
  margin: $spacing__base;
  font-size: 0.8rem;
  border: none;
  padding: 0;
  position: relative;
  // MAKE SURE YOU HAVE AN ACCESSIBE FOCUS STATE
  outline: none;
}

// Shockwave
.btn--shockwave.is-active {
  animation: shockwaveJump 1s ease-out infinite;
  
  &:after {
    @include afterBg;
    animation: shockwave 1s .65s ease-out infinite;
  }
  
  &:before {
    @include afterBg;
    animation: shockwave 1s .5s ease-out infinite;
  }
}

@keyframes shockwaveJump {
  0% {
    transform: scale(1);
  }
  40% {
    transform: scale(1.08);
  }
  50% {
     transform: scale(0.98);
  }
  55% {
    transform: scale(1.02);
  }
  60% {
    transform: scale(0.98);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes shockwave {
  0% {
    transform: scale(1);
    box-shadow: 0 0 2px rgba(0,0,0,0.15), inset 0 0 1px rgba(0,0,0,0.15);
  }
  95% {
    box-shadow: 0 0 50px rgba(0,0,0,0), inset 0 0 30px rgba(0,0,0,0);
  }
  100% {
    transform: scale(2.25);

  }
}

// JUMP
.btn--jump.is-active {
  animation: .4s jump ease infinite alternate;
}

@keyframes jump {
  0% {
    transform: scale(1);
    box-shadow: 0 1px 2px rgba(0,0,0,.15);
  }
  100% {
   ...

JavaScript

document.addEventListener('click', click);

function click(e) {
  let el
  
  el = e.target
  
  if (el !== e.currentTarget) {
    if(el.nodeName === 'BUTTON') {
      
       if(el.classList.contains('is-active')) {
         el.classList.remove('is-active')
       } else {
         el.classList.add('is-active')
       }
    }
  }
  event.stopPropagation()
}