Skip to content

Instantly share code, notes, and snippets.

@davestewart
Created January 20, 2022 23:13
Show Gist options
  • Save davestewart/d7033fb4f5851d8139049bef2326abb3 to your computer and use it in GitHub Desktop.
Save davestewart/d7033fb4f5851d8139049bef2326abb3 to your computer and use it in GitHub Desktop.
Visibly render DOM node mutations
const s = document.createElement('style')
s.setAttribute('type', 'text/css')
s.innerHTML = `
@keyframes fade {
0% { background: #FF000022; outline: 2px solid #000000; }
100% { background: #FF000000; outline: 2px solid #00000000; }
}
.updated {
animation: fade 1s linear;
}
`
document.head.appendChild(s)
let observer = new MutationObserver(mutationRecords => {
mutationRecords.forEach(record => {
const target = record.target
if (target && target.nodeType === 1) {
target.classList.add('updated')
target.addEventListener('animationend', function onAnimationEnd () {
target.classList.remove('updated')
target.removeEventListener('animationend', onAnimationEnd)
})
}
})
})
observer.observe(document.querySelector('body'), {
childList: true,
subtree: true,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment