Open button not hiding when side bar pops out

huangapple go评论71阅读模式
英文:

Open button not hiding when side bar pops out

问题

当侧边栏打开时,它会将一切推向左边。我只想要在侧边栏被推到左边时摆脱打开按钮。当侧边栏弹出时,背景图像会与打开按钮一起向左推移。我希望在单击打开按钮时使其消失,并在边距恢复正常并关闭侧边栏时重新出现。

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="script.js"></script>
    <link href="style.css" rel="stylesheet">
    <title>IKC Website</title>
</head>
<body>
    <div id="main">
    <div class="backgroundImg">
    <span class="openNav" id="openNav" onclick="openNav()">☰ open</span>
    </div>
    <div id ="sideNav" class="sideNav">
        <a id='link' href="javascript:void(0)" class="closeButton" onclick="closeNav()">×</a>
        <a id='link' href="about.html">About<a>
        <a id='link' href="members.html">Members<a>
        <a id='link' href="services.html">Services<a>
        <a id='link' href="contactUs.html">Contact<a>
    </div>
    </div>
</body>
</html>
CSS

body {
    margin: 0;
    padding: 0;
    font-family: 'Lato', sans-serif;
}

.backgroundImg {
    background-image: url(blueMoutains.jpg);
    height: 100vh;
    background-repeat: no-repeat;
    width: 100%;
    background-size: cover; 
    background-position: center;
    margin: 0;
    padding: 0;
    z-index: 1;
    filter: brightness(60%);
}

.openNav {
    font-size: 30px;
    cursor: pointer;
    position: relative;
    top: 100px;
    left: 100px;
    margin: 10px;
    z-index: 0;
    color: black;
}

.sideNav {
    position: fixed;
    top: 0;
    left: 0;
    background-color: rgb(0, 0, 0);
    width: 0px;
    height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 1;
    overflow-x: hidden;
    transition: 0.5s;
}

.sideNav a {
    color: #818181;
    text-decoration: none;
    font-size: 30px;
    padding: 10px;
    transition: 0.5s;
    display: block;
}

.sideNav .closeButton {
    color: #818181;
    width: 100px;
    margin-left: 45vh;
    position: relative;
    left: 0vh;
    bottom: 25vh;
    margin-bottom: 0;
}

.sideNav a:hover{
    color: white;
}

@media screen and (max-height: 450px) {
    .sideNav {
        padding-top: 50px;
    }
    .sideNav a {
        font-size: 18px;
    }
}

#main {
    transition: margin-left .5s;
}
Javascript

function openNav() {
    document.getElementById("sideNav").style.width = "350px";
    document.getElementById("main").style.marginLeft = "350px";
}

function closeNav() {
    document.getElementById("sideNav").style.width = "0px";
    document.getElementById("main").style.marginLeft = "0px";
}
英文:

My side bar, when opened, pushes everything to the left. I just simply want to get rid of the open button when it does get pushed to the left. When the side bar pops out, the background image gets pushed to the left with the open button. I want the open button to disappear I click it and reappear when the margins are back to normal and the side bar is closed.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="script.js"></script>
<link href="style.css" rel="stylesheet">
<title>IKC Website</title>
</head>
<body>
<div id="main">
<div class="backgroundImg">
<span class="openNav" id="openNav" onclick="openNav()">☰ open</span>
</div>
<div id ="sideNav" class="sideNav">
<a id='link' href="javascript:void(0)" class="closeButton" onclick="closeNav()">×</a>
<a id='link' href="about.html">About<a>
<a id='link' href="members.html">Members<a>
<a id='link' href="services.html">Services<a>
<a id='link' href="contactUs.html">Contact<a>
</div>
</div>
</body>
</html>

CSS


body {
margin: 0;
padding: 0;
font-family: 'Lato', sans-serif;
}
.backgroundImg {
background-image: url(blueMoutains.jpg);
height: 100vh;
background-repeat: no-repeat;
width: 100%;
background-size: cover; 
background-position: center;
margin: 0;
padding: 0;
z-index: 1;
filter: brightness(60%);
}
.openNav {
font-size: 30px;
cursor: pointer;
position: relative;
top: 100px;
left: 100px;
margin: 10px;
z-index: 0;
color: black;
}
.sideNav {
position: fixed;
top: 0;
left: 0;
background-color: rgb(0, 0, 0);
width: 0px;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 1;
overflow-x: hidden;
transition: 0.5s;
}
.sideNav a {
color: #818181;
text-decoration: none;
font-size: 30px;
padding: 10px;
transition: 0.5s;
display: block;
}
.sideNav .closeButton {
color: #818181;
width: 100px;
margin-left: 45vh;
position: relative;
left: 0vh;
bottom: 25vh;
margin-bottom: 0;
}
.sideNav a:hover{
color: white;
}
@media screen and (max-height: 450px) {
.sideNav {
padding-top: 50px;
}
.sideNav a {
font-size: 18px;
}
}
#main {
transition: margin-left .5s;
}

Javascript


function openNav() {
document.getElementById("sideNav").style.width = "350px";
document.getElementById("main").style.marginLeft = "350px";
}
function closeNav() {
document.getElementById("sideNav").style.width = "0px";
document.getElementById("main").style.marginLeft = "0px";
}

答案1

得分: 0

openNav函数内,获取打开按钮并使用样式隐藏它:

document.querySelector("#openNav").style.display = "none";

closeNav函数内再次显示它:

document.querySelector("#openNav").style.display = "initial";
英文:

Inside openNav function, get the open button and hide it using styles:

document.querySelector("#openNav").style.display = "none";

And show it again inside closeNav:

document.querySelector("#openNav").style.display = "initial";

A demo:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function openNav() {
document.getElementById(&quot;sideNav&quot;).style.width = &quot;350px&quot;;
document.getElementById(&quot;main&quot;).style.marginLeft = &quot;350px&quot;;
document.querySelector(&quot;#openNav&quot;).style.display = &quot;none&quot;;
}
function closeNav() {
document.getElementById(&quot;sideNav&quot;).style.width = &quot;0px&quot;;
document.getElementById(&quot;main&quot;).style.marginLeft = &quot;0px&quot;;
document.querySelector(&quot;#openNav&quot;).style.display = &quot;initial&quot;;
}

<!-- language: lang-css -->

body {
margin: 0;
padding: 0;
font-family: &#39;Lato&#39;, sans-serif;
}
.backgroundImg {
background-image: url(blueMoutains.jpg);
height: 100vh;
background-repeat: no-repeat;
width: 100%;
background-size: cover; 
background-position: center;
margin: 0;
padding: 0;
z-index: 1;
filter: brightness(60%);
}
.openNav {
font-size: 30px;
cursor: pointer;
position: relative;
top: 100px;
left: 100px;
margin: 10px;
z-index: 0;
color: black;
}
.sideNav {
position: fixed;
top: 0;
left: 0;
background-color: rgb(0, 0, 0);
width: 0px;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 1;
overflow-x: hidden;
transition: 0.5s;
}
.sideNav a {
color: #818181;
text-decoration: none;
font-size: 30px;
padding: 10px;
transition: 0.5s;
display: block;
}
.sideNav .closeButton {
color: #818181;
width: 100px;
margin-left: 45vh;
position: relative;
left: 0vh;
bottom: 25vh;
margin-bottom: 0;
}
.sideNav a:hover{
color: white;
}
@media screen and (max-height: 450px) {
.sideNav {
padding-top: 50px;
}
.sideNav a {
font-size: 18px;
}
}
#main {
transition: margin-left .5s;
}

<!-- language: lang-html -->

&lt;div id=&quot;main&quot;&gt;
&lt;div class=&quot;backgroundImg&quot;&gt;
&lt;span class=&quot;openNav&quot; id=&quot;openNav&quot; onclick=&quot;openNav()&quot;&gt;&amp;#9776; open&lt;/span&gt;
&lt;/div&gt;
&lt;div id =&quot;sideNav&quot; class=&quot;sideNav&quot;&gt;
&lt;a id=&#39;link&#39; href=&quot;javascript:void(0)&quot; class=&quot;closeButton&quot; onclick=&quot;closeNav()&quot;&gt;&amp;times;&lt;/a&gt;
&lt;a id=&#39;link&#39; href=&quot;about.html&quot;&gt;About&lt;/a&gt;
&lt;a id=&#39;link&#39; href=&quot;members.html&quot;&gt;Members&lt;/a&gt;
&lt;a id=&#39;link&#39; href=&quot;services.html&quot;&gt;Services&lt;/a&gt;
&lt;a id=&#39;link&#39; href=&quot;contactUs.html&quot;&gt;Contact&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月29日 09:00:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定