我想用同一个按钮隐藏和显示导航栏。我在互联网上复制了一些代码片段,并设法使用自定义动画按钮打开导航,但仍然有一个专用的关闭按钮。我如何将关闭功能映射到打开导航的同一按钮
谢谢
PS:如果你能告诉我要更改什么以便导航来自右侧而不是左侧,我将不胜感激
/*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>
最佳答案
你可以只使用.classList.toggle()
要在 css 类之间切换,请注意我删除了关闭函数和打开函数,并将宽度移至 CSS 类。
/*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>
关于javascript - 让按钮在网站上显示和隐藏导航?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62038826/