Skip to content

Instantly share code, notes, and snippets.

@diggeddy
Last active November 19, 2022 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diggeddy/cf57ecec96bc6c0efa75ac3b8bca8279 to your computer and use it in GitHub Desktop.
Save diggeddy/cf57ecec96bc6c0efa75ac3b8bca8279 to your computer and use it in GitHub Desktop.
mutationObserverAPI looking for style changes
// 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