Skip to content

Instantly share code, notes, and snippets.

@limscoder
Created November 16, 2017 19:08
Show Gist options
  • Save limscoder/863e1bf6ff9c7aae16e20a2fd849b9ea to your computer and use it in GitHub Desktop.
Save limscoder/863e1bf6ff9c7aae16e20a2fd849b9ea to your computer and use it in GitHub Desktop.
D3 animations
const userList = d3.select('ul.app-user-list');
const userSelection = userList.selectAll('li.app-user')
.data(users);
const updateUsers = userSelection;
const enterUsers = userSelection.enter();
const exitUsers = userSelection.exit();
// blend from old color to new color
updateUsers.transition()
.duration(500)
.style('color', d => (d.role === 'person' ? 'green' : 'orange'))
// render with opacity 0, then blend to opacity 1
enterUsers.append('li')
.text(d => d.name)
.style('opacity', 1)
.transition()
.duration(500)
.style('opacity', 1)
// blend to opacity 0, then remove elements
exitUsers.append('li')
.text(d => d.name)
.transition()
.duration(500)
.style('opacity', 0)
.on('end', () => exitUsers.remove())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment