Skip to content

Instantly share code, notes, and snippets.

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 telbiyski/87a76854e189c1f7c06a9f293ad6ce86 to your computer and use it in GitHub Desktop.
Save telbiyski/87a76854e189c1f7c06a9f293ad6ce86 to your computer and use it in GitHub Desktop.
Listen for CSS class change in pure JS - Mutation Observer
// ========================================== get rid of body scroll:
var mutationObserver = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    console.log(mutation);
    console.log(mutation.oldValue);
    if (mutation.oldValue === 'search-trigger') {
      document.getElementsByTagName('body')[0].classList.add('noscroll');
    } else {
      document.getElementsByTagName('body')[0].classList.remove('noscroll');
    }
  });
});

// Starts listening for changes in the '.search-trigger' HTML element
mutationObserver.observe(document.querySelector('.search-trigger'), {
  attributes: true,
  characterData: true,
  childList: true,
  subtree: true,
  attributeOldValue: true,
  characterDataOldValue: true
});
// ========================================== get rid of body scroll ENDS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment