Updated version of JS: http://jsfiddle.net/0uk0g0qq/4/ (works everywhere) the
:targetimplementation is wrong, i.e. therefore the previous one did not work. hash changes only affect css, when the user does something directly on the page, click the button that changes the hash and, unfortunately, the back button is not considered part of the page
All the javascript you need:
var menu = document.getElementById('menu');
window.addEventListener('hashchange', function(e) {
if (location.hash != "#menu") {
menu.style.display = "none";
} else if (location.hash == "#menu") {
menu.style.display = "block";
}
});
Css Only Version:
You can do this using only CSS. This is the time you learned about the selector :target.
It allows you to apply style to any fragment of the URL hash and is the identifier of the element on the page.
Demo: http://jsfiddle.net/0uk0g0qq/1
, , , . hastags url , , .
, .
#menu:target {
display: block;
}
:
body {
background-color: cornsilk;
}
.cont {
width: 500px;
margin: auto;
}
#menu {
display: none;
position: absolute;
top: 0; bottom: 0; left: 0; right: 0;
height: 100%;
background-color: rgba(0,0,0,.3);
margin: auto;
}
#menu > ul {
width: 200px;
list-style-type: none;
background-color: #fff;
margin: auto;
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%,-50%);
}
#menu > ul li {
padding: 20px 10px;
}
#menu:target {
display: block;
}