Below is the code, the problem description and questions come right after.
function showOrHideMenu(menuId) {
// alert(menuId);
var menu = document.getElementById(menuId);
// alert(menu);
var isVisible = menu.style.visibility;
// alert(isVisible);
if ((isVisible === 'hidden') || (isVisible === '')) {
// alert(menu1);
menu.style.visibility = 'visible';
menu.style.width = '75vw';
// alert("passe 1 bis");
} else {
// alert("passe 2");
menu.style.visibility = 'hidden';
// alert("passe 2 bis");
}
}
@media (min-width: 800px) {
.css_menu_1 {
visibility: visible;
font-size: 1.3rem;
background-color: goldenrod;
}
}
<img src="../images/hamburger_menu_icon.png" alt="Display or hide menu 1" class="css_btn_menu_1" id="btn_menu_1" onclick="showOrHideMenu('menu_1');">
When I am on a screen which width is less than 800px, I can click on the image.
The showOrHideMenu()
function gets executed and indeed the visibility and width of the HTML element which id is menu_1
are changed as specified.
I click a second time on the image and the HTML element gets hidden as expected.
Now, if I increase the size of the viewport, the HTML element is supposed to be visible again.
Instead of it, I get a blank space.
Does the execution of the Javascript function conflict with the "execution" of the media query?
What can I do to fix that?
-- EDIT 1 --
Adding that CSS:
.css_menu_1.active
{
visibility : visible;
width: 75vw;
}
And replacing the Javascript by this one:
<script type="text/javascript">
var btnMenu1 = document.getElementById('btn_menu_1');
var menu1 = document.getElementById('menu_1');
btnMenu1.addEventListener('click', () =>
{
menu1.classList.toggle('active');
});
</script>
solves my problem.