I think you do not have an equivalent solution in javascript, but there are ways to get convenient results.
The first form would be the combination of the event that detects changes in the width of window
, along with a check of the desired width:
window.onresize = function(event) {
if (window.innerWidth < 768) {
console.log("Largura da janela menor que 768 px");
}
};
The second way would be to use the window.matchMedia
method, according to example below:
if (window.matchMedia("(min-width: 500px)").matches) {
console.log("A viewport tem pelo menos 500 pixels de largura");
} else {
console.log("A viewport tem menos que 500 pixels de largura");
}
And through this question on SOen , I discovered that there is a plugin with the proposal to do the equivalent of CSS media queries in javascript:
link