Skip to content

Instantly share code, notes, and snippets.

@aholachek
Last active December 16, 2017 04:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aholachek/ad8987c84ec7c7fca3df9874b737af03 to your computer and use it in GitHub Desktop.
Save aholachek/ad8987c84ec7c7fca3df9874b737af03 to your computer and use it in GitHub Desktop.
React Animation Article Examples
export function animateGroups (ListComponent) {
const items = [...ListComponent.querySelectorAll('.item')]
const oldPositionDict = items.reduce((acc, item) => {
acc[item.dataset.id] = item.getBoundingClientRect()
return acc
}, {})
return function initiateAnimation () {
const transformPositionDict = {}
// Make sure to get the new array of item elements --
// React might have destroyed and created new DOM nodes
const items = [...ListComponent.querySelectorAll('.item')]
items.forEach(item => {
const oldPosition = oldPositionDict[item.dataset.id]
const newPosition = item.getBoundingClientRect()
const translateX = oldPosition.left - newPosition.left
const translateY = oldPosition.top - newPosition.top
item.style.transform = `translate(${translateX}px, ${translateY}px)`
transformPositionDict[item.dataset.id] = {
translateX: [translateX, 0],
translateY: [translateY, 0]
}
})
anime({
targets: items,
translateX: item => transformPositionDict[item.dataset.id].translateX,
translateY: item => transformPositionDict[item.dataset.id].translateY,
duration: 1000,
delay: (item, i) => i * 12,
easing: 'easeInOutElastic',
elasticity: 1
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment