Created
January 20, 2022 23:13
-
-
Save davestewart/d7033fb4f5851d8139049bef2326abb3 to your computer and use it in GitHub Desktop.
Visibly render DOM node mutations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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