Skip to content

Instantly share code, notes, and snippets.

@ctf0
Last active September 8, 2018 19:06
Show Gist options
  • Save ctf0/c3f6db8e387539cf43abd7ee233c7a9d to your computer and use it in GitHub Desktop.
Save ctf0/c3f6db8e387539cf43abd7ee233c7a9d to your computer and use it in GitHub Desktop.
<div class="item">ABC</div>
@keyframes spin {
    0% {
        transform: translate(0, 0) scale(1) rotateY(0deg);
    }

    100% {
        transform: translate(4rem, 4rem) scale(4) rotateY(540deg);
    }
}

.item {
    &.active {
        animation: spin 1s forwards;
        animation-timing-function: ease-in-out;
    }

    &.in-active {
        animation-direction: reverse
    }
}
let item = document.querySelector('.item')

// play normal
item.addEventListener('mouseover', () =>{
  item.classList.add('active')              
})

// play in reverse
item.addEventListener('mouseout', () =>{
  item.style.opacity = 0 // avoid showing the init style while switching the 'active' class
  
  item.classList.add('in-active')
  item.classList.remove('active')
  setTimeout(() => {
      item.classList.add('active')
      item.style.opacity = ''
  }, 5)

  item.addEventListener('animationend', onanimationend)
})

function onanimationend() {
    item.classList.remove('active', 'in-active')
    item.removeEventListener('animationend', onanimationend)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment