5
I’m not getting it to work, I have a retractable side menu and a button in a bar in the header. I’ve set my menu to margin-left: -250px;
and wanted that when I pressed the button it set to margin-left: 0;
, thus making the menu appear again.
CSS from the menu:
#menu-lateral {
width: 250px;
height: 100%;
margin-left: -250px;
}
HTML button:
<div class="btn-header">
<a href="" id="btn-menu">
<img src="img/menu.png">
<img src="img/icone.png">
<p>Home</p></a>
</div>
Javascript I made:
var clique = document.getElementById("btn-menu");
var menuLateral = document.getElementById("menu-lateral");
clique.onclick = function(){
document.menuLateral.style.marginLeft = "0";
};
My goal is:
When you click the button change to #menu-lateral
the margin-left: -250px;
for margin-left: 0;
and that when the menu is open, if you click again it closes (setting the margin-left: -250px;
again).
I saw it had Document there and took it, but it just flashed on the screen u.u
– Andrey Hartung
But tell me something, how I put on a toggle or something so when I click the button again it retract?
– Andrey Hartung
@Andreyhartung in this case can do so using a CSS class: http://jsfiddle.net/zjevj1v/1/
– Sergio
That’s exactly what I wanted! THANK YOU VERY MUCH! But tell me, could you explain to me how it works? I’m new to this world of web programmer :/ or a place for me to read about it would be great because I couldn’t find anything good.
– Andrey Hartung
@Andreyhartung what part did you not understand so I could explain better? or rather, what part did you understand?
– Sergio
toggle undoes what the previous click did? Why did you set ! Mportant? This Transition is only used to control the speed that the menu appears/disappears?
– Andrey Hartung
@Andreyhartung the
toggle
add a class and remove it if it already exists. It’s like the button on the TV remote control, press to turn on/off. The!important
is to say that when this class is added then this rule overrides the other ofmargin-left -250px
. Finally, Transition says that the change should be by animation and with duration of0.7
seconds– Sergio
so does this mean that it adds the toggleMenu class to the same tag as the menu-side class is? Again, thank you so much for the help!
– Andrey Hartung
@Andreyhartung he adds or removes to the element that
menuLateral
point.– Sergio