I've got small menu, which I want to automatic close on clicking anywhere on the page. I know it's probably a matter of adding one line of code, but everything I've tried so far doesn't work...
Now it is simply js function:
document.getElementById("mySidenav").style.width = "250px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
Edit: sorry, Sorry, I didn't know I could edit the first post... Here's the rest of the code:
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
body {}
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 99999;
top: 0px;
right: 0px;
background-color: #111;
overflow-x: hidden;
overflow-y: auto;
transition: 0.5s;
padding-top: 60px;
}
.sidenav a {
padding: 8px 8px 8px;
text-decoration: none;
font-size: 15px;
color: #818181;
display: block;
transition: 0.3s;
}
.sidenav a:hover {
color: #f1f1f1;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
@media screen and (max-height 450px) {
.sidenav {
padding-top: 15px;
}
.sidenav a {
font-size: 18px;
}
}
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<h2>Position #1</a>
</h2>
<h2>Position #2</a>
</h2>
<h2>Position #3</a>
</h2>
<h2>Position #4</a>
</h2>
<h2>Position #5</a>
</h2>
<h2>Position #6</a>
</h2>
<h2>Position #7</a>
</h2>
<h2>Position #8</a>
</h2>
<h2>Position #9</a>
</h2>
<h2>Position #10</a>
</h2>
</div>
<span style="font-size:20px;cursor:pointer" onclick="openNav()">☰Menu</span>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
}
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 99999;
top: 0px;
right: 0px;
background-color: #111;
overflow-x: hidden;
overflow-y: auto;
transition: 0.5s;
padding-top: 60px;
}
.sidenav a {
padding: 8px 8px 8px;
text-decoration: none;
font-size: 15px;
color: #818181;
display: block;
transition: 0.3s;
}
.sidenav a:hover {
color: #f1f1f1;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
@media screen and (max-height 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
</style>
</head>
<body>
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<h2>Position #1</a></h2>
<h2>Position #2</a></h2>
<h2>Position #3</a></h2>
<h2>Position #4</a></h2>
<h2>Position #5</a></h2>
<h2>Position #6</a></h2>
<h2>Position #7</a></h2>
<h2>Position #8</a></h2>
<h2>Position #9</a></h2>
<h2>Position #10</a></h2>
</div>
<span style="font-size:20px;cursor:pointer" onclick="openNav()">☰Menu</span>
<script>
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
</script>
</body>
</html>