P
P
Pavel Pavel2019-01-24 04:12:20
css
Pavel Pavel, 2019-01-24 04:12:20

How to close mobile menu when screen width changes?

When the screen is reduced to a certain value, the main menu disappears and a button appears to activate the menu for mobile devices. If you click on it, the menu normally opens and closes. But if, without closing it, again increase the width of the screen and then reduce it, then when you reduce it again, the menu appears in the open.
How to make it so that when changing the width it is always closed until the button is clicked? And, tell me, are there any other ways besides this to implement such a menu without js and which ones (I don’t know scripts at all)?
https://jsfiddle.net/vut2y0j1/6/

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vadim V, 2019-01-24
@reyzele

You can see how it's done here https://codepen.io/erikterwan/pen/EVzeRP, also without JS. But here's how you can uncheck a checkbox by a media query without JS, nothing comes to mind ...
Here's how you can do it in JS:

const mq  = window.matchMedia("(max-width: 700px)");
const checkbox = document.querySelector('#burger-active');

window.addEventListener('resize', function(){
if(!mq.matches && checkbox.checked) {
  checkbox.checked = false;
}
});

A
Alex ALex, 2019-01-24
@Alex1265

Use media queries.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question