const openBtn = document.querySelector('.open')
const closeBtn = document.querySelector('.close')
const navContainer = document.querySelector('.nav-container')
openBtn.addEventListener('click', () => {
openBtn.classList.add('hide')
closeBtn.classList.remove('hide')
navContainer.classList.remove('.show-nav')
})
closeBtn.addEventListener('click', () => {
closeBtn.classList.add('hide')
openBtn.classList.remove('hide')
navContainer.classList.add('.show-nav')
})
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
*{
margin: 0;
padding: 0;
box-sizing: 0;
font-family: 'Roboto', sans-serif;
}
a{
text-decoration: none;
color: black;
}
li{
list-style: none;
}
.nav-btn{
position: absolute;
right: 0%;
border: none;
background: transparent;
margin: 10px;
cursor: pointer;
}
.hide{
display: none;
}
body{
overflow: hidden;
}
.nav-container{
display: flex;
flex-direction: column;
width: 100%;
height: 100vh;
transform: translateX(-100%);
transition: all 0.2s ease;
}
.show-nav{
transform: translateX(0%);
}
h1{
margin: 10px;
font-size: 40px;
}
h1 span{
color: cornflowerblue;
}
.links{
margin: 10px;
margin-left: 24px;
font-size: 32px;
}
.links a{
line-height: 48px;
}
.social{
display: flex;
flex-direction: row;
position: absolute;
bottom: 0%;
margin-bottom: 10px;
}
.social a{
margin-right: 6px;
margin-left: 24px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<script defer src="main.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.14.0/css/all.css" integrity="sha384-HzLeBuhoNPvSl5KYnjx0BT+WB0QEEqLprO+NBkkk5gbc67FTaL7XIGa2w1L0Xbgc" crossorigin="anonymous">
<title>Document</title>
</head>
<body>
<button class="nav-btn open">
<i class="fas fa-bars fa-3x"></i>
</button>
<button class="nav-btn close hide">
<i class="fas fa-times fa-3x"></i>
</button>
<nav class="nav-container">
<h1>
Side
<span>
Bar
</span>
</h1>
<ul class="links">
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">About</a>
</li>
<li>
<a href="#">Projects</a>
</li>
<li>
<a href="#">Contacts</a>
</li>
</ul>
<ul class="social">
<li>
<a href="#">
<i class="fab fa-twitter fa-lg"></i>
</a>
</li>
<li>
<a href="#">
<i class="fab fa-youtube fa-lg"></i>
</a>
</li>
<li>
<a href="#">
<i class="fab fa-github fa-lg"></i>
</a>
</li>
</ul>
</nav>
</body>
</html>
I attempted to create a sidebar using HTML, CSS, and JavaScript but encountered an issue with the sidebar not displaying correctly. My goal is to show the sidebar by adding a '.show-nav' class that resets the '.nav-container' transform property to translate back to 0%. Although the '.show-nav' class is being added, the properties within it do not update the style as intended.