0
Hello
I was trying to make a button open and close a fixed menu with javascript but I don’t know what I’m doing wrong with my code that doesn’t work the way it should.
In theory, when the width of the div is greater than or equal to 75px, the button onClick The function of close-up if it did not have the function of open.
Code:
/* menu fixo */
var open = document.getElementById("menu-fixo").style.width;
function toggle() {
if (open >= "75px") {
document.getElementById("botao-abrir-menu-fixo").onclick = closeNav();
} else {
document.getElementById("botao-abrir-menu-fixo").onclick = openNav();
}
}
function openNav() {
document.getElementById("menu-fixo").style.width = "75px";
document.getElementById("botao-abrir-menu-fixo").style.right = "75px";
}
function closeNav() {
document.getElementById("menu-fixo").style.width = "0";
document.getElementById("botao-abrir-menu-fixo").style.right = "0";
}
#menu-fixo {
position: fixed;
top: 50%;
right: 0;
transform: translateY(-50%);
width: 75px;
min-width: 0;
min-height: 150px;
background-color: blue;
z-index: 100;
transition: width 2s;
}
#botao-abrir-menu-fixo {
position: fixed;
top: 50%;
right: 75px;
transform: translateY(-50%);
min-width: 75px;
min-height: 75px;
background-color: blue;
z-index: 100;
display: block;
transition: right 2s;
}
<div id="menu-geral">
<div id="menu-fixo">menu</div>
<div id="botao-abrir-menu-fixo" onclick="toggle()">abrir</div>
</div>
Worked thanks!
– David Mv