Reputation: 41
I want to hide and show the nav bar with the same button. I copied some code snippets around the interwebs and managed to oben the navigation with a custom animated button but there is stil a dedicated close button. how do i map the close function to the same button that opens the nav
thank you
PS: if you can tell me what to change so the navigation comes from the right not the left id be very gratefull
/*bar animation bar to cross*/
function myFunction(x) {
x.classList.toggle("change");
}
/* Set the width of the side navigation to 250px */
function openNav() {
document.getElementById("mySidenav").style.width = "500px";
}
/* Set the width of the side navigation to 0 */
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
/* Header */
/* Style the header with a grey background and some padding */
.header {
overflow: hidden;
background-color: #FFFFFF;
padding-top: 40px;
padding-right: 10px;
padding-left: 10px;
padding-bottom: 50px;
box-shadow: 0 4px 5px 0 rgba(0, 0, 0, 0.2);
}
/* Style the header links */
.header a {
float: left;
color: black;
text-align: center;
padding-top: 12px;
padding-right: 12px;
padding-left: 12px;
padding-bottom: 12px;
text-decoration: none;
font-family: lato;
font-size: 18px;
line-height: 25px;
border-radius: 4px;
}
.menu_bar_icon {
display: inline-block;
cursor: pointer;
}
.bar1,
.bar2,
.bar3 {
width: 20px;
height: 2px;
background-color: #000000;
margin: 4px 0;
transition: 0.4s;
}
/* Rotate first bar */
.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-4px, 3px);
transform: rotate(-45deg) translate(-4px, 3px);
}
/* Fade out the second bar */
.change .bar2 {
opacity: 0;
}
/* Rotate last bar */
.change .bar3 {
-webkit-transform: rotate(45deg) translate(-5px, -4px);
transform: rotate(45deg) translate(-5px, -4px);
}
/* The side navigation menu */
.sidenav {
height: 90px;
/* 100% Full-height */
width: 0;
/* 0 width - change this with JavaScript */
position: fixed;
/* Stay in place */
z-index: 1;
/* Stay on top */
top: 0;
/* Stay at the top */
left: 70%;
overflow-x: hidden;
/* Disable horizontal scroll */
padding-top: 50px;
/* Place content 60px from the top */
transition: 0.5s;
/* 0.5 second transition effect to slide in the sidenav */
}
/* The navigation menu links */
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 15px;
color: #000000;
display: block;
transition: 0.3s;
}
/* When you mouse over the navigation links, change their color */
.sidenav a:hover {
color: #E20105;
}
/* Position and style the close button (top right corner) */
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
/* Style page content - use this if you want to push the page content to the right when you open the side navigation */
#main {
transition: margin-left .5s;
padding: 20px;
}
/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
@media screen and (max-height: 450px) {
.sidenav {
padding-top: 15px;
}
.sidenav a {
font-size: 18px;
}
}
/* Style the logo link (notice that we set the same value of line-height and font-size to prevent the header to increase when the font gets bigger */
.header a.logo {
font-size: 130px;
font-weight: bold;
}
/* Change the background color on mouse-over */
.header a:hover {
background-color: #ddd;
color: black;
}
/* Style the active/current link*/
.header a.active {
background-color: dodgerblue;
color: white;
}
/* Float the link section to the right */
.header-right {
float: right;
}
/* Add media queries for responsiveness - when the screen is 500px wide or less, stack the links on top of each other */
@media screen and (max-width: 500px) {
.header a {
float: none;
display: block;
text-align: left;
}
.header-right {
float: none;
}
}
#container {
width: 100%;
/* Your width */
height: 100%;
/* Your height */
display: flex;
justify-content: center;
/* This will center container with items horizontally */
align-items: center;
/* This will center container with items vertically */
/* outline: 5px dashed yellow;
*/
}
<link href="http://fonts.googleapis.com/css?family=Lato:400,700" rel="stylesheet" type="text/css">
<div class="header">
<a href="#default" class="logo">Kunden;</a>
<div class="header-right">
<div class="menu_bar_icon" onclick="myFunction(this), openNav()">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
</div>
</div>
Upvotes: 3
Views: 1555
Reputation: 11622
You can just use .classList.toggle()
to toggle between css classes, notice that I removed the close function and open function, and moved the width to CSS class.
/*html, body {
max-width: 100%;
max-height: 100%;
overflow-x: hidden;
overflow-y: hidden;
}
#fullbodybg{
position: absolute;
width: 170%;
height: 100%;
overflow: visible;
background: linear-gradient(
110deg,
#ffffff 50%,
#000000 50%);
outline: 5px dashed blue;
}*/
/* Header */
/* Style the header with a grey background and some padding */
.header {
overflow: hidden;
background-color: #FFFFFF;
padding-top: 40px;
padding-right: 10px;
padding-left: 10px;
padding-bottom: 50px;
box-shadow: 0 4px 5px 0 rgba(0, 0, 0, 0.2);
}
/* Style the header links */
.header a {
float: left;
color: black;
text-align: center;
padding-top: 12px;
padding-right: 12px;
padding-left: 12px;
padding-bottom: 12px;
text-decoration: none;
font-family: lato;
font-size: 18px;
line-height: 25px;
border-radius: 4px;
}
.menu_bar_icon {
display: inline-block;
cursor: pointer;
position: relative;
z-index: 1;
}
.bar1, .bar2, .bar3 {
width: 20px;
height: 2px;
background-color: #000000;
margin: 4px 0;
transition: 0.4s;
}
/* Rotate first bar */
.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-4px, 3px) ;
transform: rotate(-45deg) translate(-4px, 3px) ;
}
/* Fade out the second bar */
.change .bar2 {
opacity: 0;
}
/* Rotate last bar */
.change .bar3 {
-webkit-transform: rotate(45deg) translate(-5px, -4px) ;
transform: rotate(45deg) translate(-5px, -4px) ;
}
/* The side navigation menu */
.sidenav {
height: 90px;
width: 352px;
position: fixed;
z-index: 1;
top: 0;
left: 70%;
overflow-x: hidden;
padding-top: 50px;
transition: 0.5s;
width: 500px;
right: -540px;
left: auto;
}
/* The navigation menu links */
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 15px;
color: #000000;
display: block;
transition: 0.3s;
}
/* When you mouse over the navigation links, change their color */
.sidenav a:hover {
color: #E20105;
}
/* Position and style the close button (top right corner) */
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
/* Style page content - use this if you want to push the page content to the right when you open the side navigation */
#main {
transition: margin-left .5s;
padding: 20px;
}
/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
/* Style the logo link (notice that we set the same value of line-height and font-size to prevent the header to increase when the font gets bigger */
.header a.logo {
font-size: 130px;
font-weight: bold;
}
/* Change the background color on mouse-over */
.header a:hover {
background-color: #ddd;
color: black;
}
/* Style the active/current link*/
.header a.active {
background-color: dodgerblue;
color: white;
}
/* Float the link section to the right */
.header-right {
float: right;
}
/* Add media queries for responsiveness - when the screen is 500px wide or less, stack the links on top of each other */
@media screen and (max-width: 500px) {
.header a {
float: none;
display: block;
text-align: left;
}
.header-right {
float: none;
}
}
#container{
width: 100%;/* Your width */
height: 100%;/* Your height */
display: flex;
justify-content: center; /* This will center container with items horizontally */
align-items: center; /* This will center container with items vertically */
/* outline: 5px dashed yellow;
*/
}
#mySidenav.open {
right: 0;
z-index: 0;
left: auto;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HFFoto - Portfolio</title>
<link rel="stylesheet" type="text/css" id="applicationStylesheet" href="portfolio_kunden.css"/>
<link href="http://fonts.googleapis.com/css?family=Lato:400,700" rel="stylesheet" type="text/css">
<style type="text/css">
</style>
<script>
/*bar animation bar to cross*/
function myFunction(x) {
x.classList.toggle("change");
document.getElementById("mySidenav").classList.toggle('open');
}
</script>
</head>
<body>
<div class="header">
<a href="#default" class="logo">Kunden;</a>
<div class="header-right">
<div class="menu_bar_icon" onclick="myFunction(this)">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
<div id="mySidenav" class="sidenav">
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
</div>
</div>
Upvotes: 1
Reputation: 1625
function navToggle() {
var ele = document.getElementById("mySidenav");
if (ele.classList.contains("isVisible")) {
ele.classList.remove("isVisible");
ele.style.display = "none";
} else {
ele.classList.add("isVisible");
ele.style.display = "block";
}
}
<div class="menu_bar_icon" onclick="navToggle()">
Click Me!
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
<div id="mySidenav" class="sidenav isVisible">
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<div class="menu_bar_icon" onclick="navToggle()">
Click Me!
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
<div id="mySidenav" class="sidenav isVisible">
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
function navToggle() {
var ele = document.getElementById("mySidenav");
if (ele.classList.contains("isVisible")) {
ele.classList.remove("isVisible");
ele.style.display = "none";
} else {
ele.classList.add("isVisible");
ele.style.display = "block";
}
}
Upvotes: 0
Reputation: 177950
This could work, but I do not know what you want to click after opening the menu
window.addEventListener("load",function() {
document.querySelector(".menu_bar_icon").addEventListener("click",function() {
this.classList.toggle("change");
document.getElementById("mySidenav").style.width = this.classList.contains("change") ? "500px" : 0;
})
})
Upvotes: 1