Last active
November 19, 2022 10:10
-
-
Save diggeddy/cf57ecec96bc6c0efa75ac3b8bca8279 to your computer and use it in GitHub Desktop.
mutationObserverAPI looking for style changes
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
// simple mutation observer | |
// listen to group of elements with target-class | |
// for style change and toggle class on root HTML | |
var modals = document.querySelectorAll('.target-class'); | |
var rootHTML = document.getElementsByTagName( 'html' )[0]; | |
var observer = new MutationObserver(function(mutations) { | |
mutations.forEach(function(mutationRecord) { | |
// do something on observed style change | |
rootHTML.classList.toggle('.toggle-class'); | |
}); | |
}); | |
modals.forEach(modal => { | |
observer.observe(modal, { | |
attributes: true, | |
attributeFilter: ['style'] | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment