Skip to content

Instantly share code, notes, and snippets.

@nightsh
Last active November 22, 2019 06:35
Show Gist options
  • Save nightsh/751215991c7c21a30ea608cbbf6a425a to your computer and use it in GitHub Desktop.
Save nightsh/751215991c7c21a30ea608cbbf6a425a to your computer and use it in GitHub Desktop.
javascript style observer
var styleObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutationRecord) {
var computedStyle = window.getComputedStyle(target, null);
console.log(mutationRecord);
if (mutationRecord.target.style.display == 'block') {
console.log('block!');
mutationRecord.target.focus();
}
});
});
// By class name
var targets = document.getElementsByClassName("modal");
var i;
for (i = 0; i < targets.length; i++) {
styleObserver.observe(targets[i], { attributes : true, attributeFilter : ['style'] });
}
// By id
var target = document.getElementById('embed-{{ resource_view['id'] }}');
styleObserver.observe(target, { attributes : true, attributeFilter : ['style'] });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment