This is a submission for DEV Challenge v24.03.20, One Byte Explainer: Browser API or Feature.
Explainer
matchMedia, which exists on the...
For further actions, you may consider blocking this person and/or reporting abuse
What do you mean using change event in this context?
document.addEventListener('change', () => {
window.matchMedia('(max-width: 480px)') {
// do something
}
})
?
Not quite you have to put the event listener on the returned
matchMediainstance, like this.Here's a quick example you can play with
Very nice :) Thank you. I suppose this is more optimal than window resize event ? :)
Very much so! It's why I love it so much it's fast and only fires when the media changes so no need to debounce.