I'm wondering if there is a way to create a method or use an event listener that will be triggered when a CSS change occurs.
Specifically, my stylesheet includes media queries and I am interested in knowing if it's possible to attach a listener to detect when those media queries become active or inactive. For instance, I have a media query set up to hide a button at certain screen widths:
@media screen and (max-width: 480px) {
#search-button {
display: none;
}
}
Is there an event listener that could help me determine when the display property changes due to the media query? Currently, I am using the following approach:
$(window).resize(function() {
if($('#search-button').css("display") == "none") {
//do something
} else {
//do something else
}
});
Although this method works fine, it triggers every time the user adjusts the screen size. I would prefer it to only fire when the CSS of the button actually changes. I hope that clarifies my question.
For instance, I would like something similar to the following:
$('#search-button').cssEventListenerOfSomeKind(function() {
alert('the display changed');
});